Files
TBgen_App/utils/__pycache__/utils.cpython-312.pyc

110 lines
12 KiB
Plaintext
Raw Normal View History

2026-03-30 16:46:48 +08:00
<EFBFBD>
P<>Yi<59> <00><00>(<00>dZddlZddlZddlZddlZddlZddlZddlmZddl m
Z
ddlmZm Z dde fd<08>Z dd <09>Zd
<EFBFBD>ZGd <0B>d <0C>Zdd <0A>Zd<0E>ZGd<0F>d<10>Zd d<11>Zedd<13>Zedd<15>Zedd<17>Zedd<18>Zdd<19>Zd!de ezde fd<1B>Zd<1C>Zy)"z<>
Description : some utils for project
Author : Ruidi Qiu (ruidi.qiu@tum.de)
Time : 2022/11/08 13:00:00
LastEdited : 2024/9/3 17:33:26
<EFBFBD>N<><01>wraps)<01>repeat)<02>datetime<6D> timedelta<74><00>returnc<00><><00>t|<00>dk(ryd}|D]>}t|t<00>r |d|zz }<02>t|t<00>r |d|zz }<02>5|d||fzz }<02>@|ddd z}|S)
a
convert a list of string/number to a string;
to show the list in the way what we see it in the code
if string, add '' around it; if number, do nothing
Example:
::
str_list(['a', 2, '3']) -> "['a', 2, '3']"
rz[]<5D>[z'%s', z%d, z%.*f, N<><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>])<04>len<65>
isinstance<EFBFBD>str<74>int)<04>list<73> precision<6F>str_list<73>is <20>1/home/zhang/CorrectBench/TBgen_App/utils/utils.pyrrs<><00><00> <0B>4<EFBFBD>y<EFBFBD>A<EFBFBD>~<7E><13><12>H<EFBFBD> <11>2<><01> <15>a<EFBFBD><13> <1D> <14><08>A<EFBFBD><0E> &<26>H<EFBFBD> <17><01>3<EFBFBD> <1F> <14><06>!<21> <0C> $<24>H<EFBFBD> <14><08>I<EFBFBD>q<EFBFBD>><3E>1<> 1<>H<EFBFBD> 2<><18><03><12>}<7D>s<EFBFBD>"<22>H<EFBFBD> <13>O<EFBFBD>c<00><00><00><00>fd<01>}|S)z<>
print the running time of a function
For example:
::
@print_time()
def forward(self, input):
return self.top_level(input)
c<00><00><01><00><00><02>fd<01>}|S)Nc<00><><00><02><00>rmtj<00>}<00>|i|<01><01>}t<00><07>jd<01>d}tj<00>|z
}tdj ||<05><00>|S<00>|i|<01><01>}|S)N<> <20>z{} use time: {}s)<05>timer<00>split<69>print<6E>format)<08>args<67>kwargs<67>old_time<6D>result<6C> func_name<6D>run_time<6D>en<65>funcs <20><>r<00>wrapperz5print_time.<locals>.decorator_nopara.<locals>.wrapper9sz<00><><00><11><1F>9<EFBFBD>9<EFBFBD>;<3B><08><1D>t<EFBFBD>.<2E>v<EFBFBD>.<2E><06><1F><04>I<EFBFBD>O<EFBFBD>O<EFBFBD>C<EFBFBD>0<><11>3<> <09><1F>9<EFBFBD>9<EFBFBD>;<3B><18>1<><08><15>(<28>/<2F>/<2F> <09>8<EFBFBD>D<>E<><1A>M<EFBFBD><1E>t<EFBFBD>.<2E>v<EFBFBD>.<2E><06><19>Mr<00>)r(r)r's` <20>r<00>decorator_noparaz$print_time.<locals>.decorator_nopara8s<00><><00> <1A><17>rr*)r'r+s` r<00>
print_timer,-s<00><><00> <17> <1C>rc<00><00><00><00>fd<01>}|S)z5
decorator
raise error after a function
c<00>*<00><01><00>|i|<01><01>td<01><00>)Nz5this error is raised by debug decorator "raise_error")<01> Exception)r!r"r(s <20>rr)zraise_error.<locals>.wrapperMs<00><><00> <0C>d<EFBFBD><1D>f<EFBFBD><1D><17>O<>P<>Prr*)r(r)s` r<00> raise_errorr0Gs<00><><00> Q<01> <13>Nrc<00>d<00>eZdZdZd
d<03>Zd<04>Zd<05>Zed<06><00>Zed<07><00>Z ed<08><00>Z
ed <09><00>Z y) <0B>TimeraR
print the running time of a code block
Args:
- code_name (str): the name of the code block; default: None
- print_en (bool): whether to print the running time; default: True
Example 1 (print time on the console):
::
with Timer('test') as t:
loss.backward() # some code
# this will print 'test: time cost = 0.1s' on the console
Example 2 (get time of a code block):
::
with Timer(print_en=False) as t:
loss.backward() # some code
time_cost = t.interval # time_cost = 0.1
Nc<00> <00>||_||_y<00>N)<02> code_name<6D>print_en)<03>selfr5r6s r<00>__init__zTimer.__init__ks<00><00>"<22><04><0E> <20><04> rc<00>8<00>tj<00>|_|Sr4)r<00>start<72>r7s r<00> __enter__zTimer.__enter__os<00><00><19>Y<EFBFBD>Y<EFBFBD>[<5B><04>
<EFBFBD><13> rc<00><00>tj<00>|_|j|jz
|_d|jz}|j<00>|jdz|z}|j
r t |<02>||_y)Nztime cost = %.4fsz: )r<00>endr:<00> interval_timer5r6r<00>
print_line)r7r!r@s r<00>__exit__zTimer.__exit__ssg<00><00><17>9<EFBFBD>9<EFBFBD>;<3B><04><08>!<21>X<EFBFBD>X<EFBFBD><04>
<EFBFBD>
<EFBFBD>2<><04><1A>(<28>$<24>*<<3C>*<<3C>=<3D>
<EFBFBD> <0F>><3E>><3E> %<25><1D><1E><1E>$<24>.<2E><1A>;<3B>J<EFBFBD> <0F>=<3D>=<3D> <11>*<2A> <1D>$<24><04>rc<00><00>|jSr4)r?r;s r<00>intervalzTimer.interval}s<00><00><13>!<21>!<21>!rc<00><00>|jSr4)r5r;s r<00>namez
Timer.name<6D>s <00><00><13>~<7E>~<7E>rc<00><00>|jSr4<00>r@r;s r<00>infoz
Timer.info<66><00> <00><00><13><EFBFBD><EFBFBD>rc<00><00>|jSr4rGr;s r<00>messagez Timer.message<67>rIr)NT) <0C>__name__<5F>
__module__<EFBFBD> __qualname__<5F>__doc__r8r<rA<00>propertyrCrErHrKr*rrr2r2Ssf<00><00><08>.!<21><14>%<25><0E>"<22><0E>"<22><0E><1E><0E><1E><0E><1F><0E><1F><0E><1F><0E>rr2c<00><><00>|r
t<00>Stjdtjtj<00><00><00>S)zE
get the string of current time, format: '%H:%M:%S %Y-%m-%d'
z%H:%M:%S %Y-%m-%d)<04>get_time_compactr<00>strftime<6D> localtime)<01>compacts r<00>get_timerV<00>s1<00><00><0F><1F>!<21>!<21><13>}<7D>}<7D>0<>$<24>.<2E>.<2E><14><19><19><1B>2M<32>N<>Nrc<00>P<00>tj<00>}|jd<01>}|S)Nz %Y%m%d_%H%M%S)r<00>nowrS)rX<00>time_strs rrRrR<00>s <00><00>
<12>,<2C>,<2C>.<2E>C<EFBFBD><12>|<7C>|<7C>O<EFBFBD>,<2C>H<EFBFBD> <13>Orc<00>"<00>eZdZdZd<02>Zd<03>Zd<04>Zy)<06>
run_in_dirz<EFBFBD>
change the current directory to a new directory, and then change it back after the code block
Args:
dir (str): the new directory (relative path to the current directory)
c<00><00>||_yr4)<01>new_dir_relative)r7<00>dirs rr8zrun_in_dir.__init__<5F>s
<00><00> #<23><04>rc<00><><00>tj<00>|_tjj |j|j
<00>|_tj|j <00>yr4)<08>os<6F>getcwd<77>old_dir<69>path<74>joinr]<00>new_dir<69>chdirr;s rr<zrun_in_dir.__enter__<5F>s?<00><00><19>y<EFBFBD>y<EFBFBD>{<7B><04> <0C><19>w<EFBFBD>w<EFBFBD>|<7C>|<7C>D<EFBFBD>L<EFBFBD>L<EFBFBD>$<24>2G<32>2G<32>H<><04> <0C>
<EFBFBD><08><08><14><1C><1C>rc<00>B<00>tj|j<00>yr4)r`rfrb)r7r!s rrAzrun_in_dir.__exit__<5F>s<00><00>
<EFBFBD><08><08><14><1C><1C>rN)rLrMrNrOr8r<rAr*rrr[r[<00>s<00><00><08> $<24><1F>rr[c<00> <00><00><00>fd<01>}||_|S)Nc<00><><00><01>t|tjj<00>r t |<00>St t |<00><01><00>Sr4)r<00> collections<6E>abc<62>Iterable<6C>tupler)<02>x<>ns <20>r<00>parsez_ntuple.<locals>.parse<73>s1<00><><00> <15>a<EFBFBD><1B><1F><1F>1<>1<> 2<><18><11>8<EFBFBD>O<EFBFBD><14>V<EFBFBD>A<EFBFBD>q<EFBFBD>\<5C>"<22>"r)rL)rorErps` r<00>_ntuplerq<00>s<00><><00>#<23>
<1A>E<EFBFBD>N<EFBFBD> <10>Lrr<00>_single<6C><00>_pair<69><00>_triple<6C>
_quadruplec<00><00>d}tj|<00>D]e\}}}|D]Z}|dk(s<01> tjtjj ||<06><00>|s<01>?|dz }|dzdk(s<01>Mt d|z<00><00>\<00>gy)z:
remove all the "wave.vcd" files in the directory
rzwave.vcdr<00>dz%d files cleanedN)r`<00>walk<6C>removercrdr)<07> clean_dir<69>cnt_en<65>cnt<6E>root<6F>dirs<72>files<65>files r<00>clean_wave_vcdr<64><00>s<00><00> <0C>C<EFBFBD><1F>W<EFBFBD>W<EFBFBD>Y<EFBFBD>/<2F>:<3A><19><04>d<EFBFBD>E<EFBFBD><19> :<3A>D<EFBFBD><13>z<EFBFBD>!<21><12> <09> <09>"<22>'<27>'<27>,<2C>,<2C>t<EFBFBD>T<EFBFBD>2<>3<><19><17>1<EFBFBD>H<EFBFBD>C<EFBFBD><1A>S<EFBFBD>y<EFBFBD>A<EFBFBD>~<7E><1D>0<>C<EFBFBD>8<>9<> :<3A>:r<00> start_dayc<00>\<00>dddddddd<08>}t|t<00>r||n|d z}|<01>tj<00>}ntj|d
<EFBFBD>}|j <00>}||z
d z}|t |<04> <0B>z
}|t d<07> <0B>z}|jd <0C>d z|jd <0C>zS)a{
- function:
- return the week range of the current week, the start day can be any day of the week
- for example, if today is 20240807, which is wednesday, if the start_day is "Monday", the output will be "0805~0811"; if the start day is "Tuesday", the output will be "0806~0812"; if the start day is "Thursday", the output will be "0801~0807"
- input:
- start_day: the start day of the week, can be a string or an integer
- string: the name of the day, for example, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
- integer: the index of the day, 0 is Monday, 1 is Tuesday, 2 is Wednesday, 3 is Thursday, 4 is Friday, 5 is Saturday, 6 is Sunday, invalid index will be mod 7
- today: the date of the day, if None, the current date will be used;
- formart: "%Y%m%d", e.g. "20240807"
rrrsrur<00><00>)<07>Monday<61>Tuesday<61> Wednesday<61>Thursday<61>Friday<61>Saturday<61>Sunday<61>z%Y%m%d)<01>daysz%m%d<>~)rrr<00>today<61>strptime<6D>weekdayrrS)r<>r<><00> weekday_map<61>current_weekday<61>days_to_subtractr:r>s r<00>get_week_ranger<65><00>s<><00><00><1F>1<EFBFBD>1<EFBFBD>!<21>WX<57>fg<66>st<73>u<>K<EFBFBD>*4<>Y<EFBFBD><03>*D<> <0B>I<EFBFBD>&<26>)<29>VW<56>-<2D>I<EFBFBD> <0A>}<7D><18><0E><0E> <20><05><18>!<21>!<21>%<25><18>2<><05><1C>m<EFBFBD>m<EFBFBD>o<EFBFBD>O<EFBFBD>(<28>)<29>3<>q<EFBFBD>8<><14> <12>I<EFBFBD>#3<>4<> 4<>E<EFBFBD> <10>)<29><11>#<23>
#<23>C<EFBFBD> <10>><3E>><3E>&<26> !<21>C<EFBFBD> '<27>#<23>,<2C>,<2C>v<EFBFBD>*><3E> ><3E>>rc<00><00><00><00>fd<01>}|S)Nc<00>2<00><01><00>t<00><00><00><00>fd<01><08>}|S)Nc<00><00><02><00><01><00><00><05>fd<01>}g}tj||f<01><02>}|j<00>|j<00><06>|j <00>rt d<03><06>d<04><03><00>t |dt<00>r|d<00>|dS)Nc<00><><00><03> |j<00><03>i<00><04><01><01>y#t$r}|j|<01>Yd}~yd}~wwxYwr4)<02>appendr/)r$<00>er!r(r"s <20><><EFBFBD>r<00>targetzDrun_with_timeout.<locals>.decorator.<locals>.wrapper.<locals>.target<65>s=<00><><00>%<25><1A>M<EFBFBD>M<EFBFBD>$<24><04>"7<><06>"7<>8<><38> <20>%<25><1A>M<EFBFBD>M<EFBFBD>!<21>$<24>$<24><>%<25>s<00><00> ?<03>:<03>?)r<>r!zFunction call timed out after z secondsr)<08> threading<6E>Threadr:rd<00>is_alive<76> TimeoutErrorrr/)r!r"r<>r$<00>threadr(<00>timeouts`` <20><>rr)z4run_with_timeout.<locals>.decorator.<locals>.wrapper<65>s~<00><><00> %<25><18>F<EFBFBD><1E>%<25>%<25>V<EFBFBD>6<EFBFBD>)<29>D<>F<EFBFBD> <12>L<EFBFBD>L<EFBFBD>N<EFBFBD> <12>K<EFBFBD>K<EFBFBD><07> <20><16><EFBFBD><EFBFBD> <20>"<22>%C<>G<EFBFBD>9<EFBFBD>H<EFBFBD>#U<>V<>V<><1A>&<26><11>)<29>Y<EFBFBD>/<2F><1C>Q<EFBFBD>i<EFBFBD><0F><1A>!<21>9<EFBFBD> rr)r(r)r<>s` <20>r<00> decoratorz#run_with_timeout.<locals>.decorator<6F>s <00><><00> <0E>t<EFBFBD><1B> <1D>
<15> <1D>8<17>rr*)r<>r<>s` r<00>run_with_timeoutr<74><00>s<00><><00><17>> <15>r)r)T)F)rp)r<>N)rOrrrjr`<00>tiktokenr<6E><00> functoolsr<00> itertoolsrrrrr,r0r2rVrRr[rqrrrtrvrwr<>rr<>r<>r*rr<00><module>r<>s<><00><01><04>  <0C><0F><12> <09><0F><10><1B><1C>(<28><14>3<EFBFBD><14>8<1C>4 <13>8<1F>8<1F>tO<01><14>
<1F><1F>"<11> <12>!<21>Y<EFBFBD>
<1F><07><0F><01>7<EFBFBD><1B><05>
<11>!<21>Y<EFBFBD>
<1F><07> <14>Q<EFBFBD> <0C> %<25>
<EFBFBD> :<3A> ?<3F>S<EFBFBD><13>W<EFBFBD> ?<3F>C<EFBFBD> ?<3F>D r