Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2bf9895

Browse files
committed
Cleanup
1 parentcf73671 commit2bf9895

File tree

9 files changed

+169
-86
lines changed

9 files changed

+169
-86
lines changed

‎python_algorithms/algorithms/search/regex.py‎

Whitespace-only changes.

‎python_algorithms/enumerations/comprehensions.py‎

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#### Dict Comprehension
2+
3+
my_dict= {'a':1,'b':2,'c':3}
4+
result= {k+'_key':v*2fork,vinmy_dict.items()}
5+
print(result)
6+
print(result.keys())
7+
print(result.values())
8+
print(result.items())
9+
10+
# filtering with dict comprehension
11+
fruits= ['apple','mango','banana','cherry']
12+
13+
f1={f:f.capitalize()forfinfruits}
14+
remove_this= {'apple','cherry'}
15+
# dict comprehension example to delete key:value pairs
16+
{key:f1[key]forkeyinf1.keys()-remove_this}
17+
#=> {'banana': 'Banana', 'mango': 'Mango'}

‎python_algorithms/enumerations/iterators/itertooling.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
fromitertoolsimportcombinations,permutations
44
fromitertoolsimportproduct,count,compress
55
fromfunctoolsimportreduce
6-
7-
86
# from itertools import ifilter
97
# from itertools import imap
108
# from itertools import izip
119

12-
1310
## groupby
1411
mylist='aaaaabbbbbccdd'#sorted
1512
# group returns a generator
@@ -27,6 +24,7 @@
2724
print({k:list(v)fork,vingroupby(mylist,
2825
key=lambdax:x['group'])})
2926

27+
3028
mylist= [
3129
{'id':1,'name':'ray' },
3230
{'id':1,'email':'ray@hotmail.com' },
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## List Comprehensions
2+
matrix= [[1,2,3], [4,5,6]]
3+
4+
# for loop
5+
negative_matrix= []
6+
forrowinmatrix:
7+
negative_matrix.append([-nforninrow])
8+
9+
# list comprehension
10+
negative_evens_matrix= [
11+
[-nforninrow]# or negate(n)
12+
forrowinmatrix
13+
ifn%2==0
14+
]
15+
16+
fromitertoolsimportizip
17+
list(zip(*matrix))# returns tuples
18+
print([list(col)forcolinizip(*matrix)])
Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,69 @@
11
# Enter your code here. Read input from STDIN. Print output to STDOUT
2-
kilos_to_pounds=lambdap:2.20462*p
3-
pounds_to_kilos=lambdak:0.453592*k
4-
pounds_to_grams=lambdap:p/1000
5-
kilos_to_grams=lambdak:k*1000
6-
meters_to_feet=lambdam:3.28084*m
7-
feet_to_meters=lambdaf:0.3048*f
8-
farenheit_to_celsius=lambdaf: (f-32.0)* (5.0/9.0)
9-
celsius_to_farenheit=lambdac: (c*9.0/5.0)+32.0
10-
112
importsys
123
importre
134

14-
thismodule=sys.modules[__name__]
15-
whitelist= {
16-
'pounds':'kilos',
17-
'kilos':'pounds',
18-
'meters':'feet',
19-
'feet':'meters',
20-
'farenheit':'celsius',
21-
'celsius':'farenheit',
22-
'volts':'gigavolts'
23-
}
24-
defis_allowed(source,dest):
25-
allowed= {
26-
source:whitelist.get(source,None)
27-
fork,vinwhitelist.items()
28-
ifsourceinwhitelistandwhitelist[source]==v
29-
}
5+
classConvert:
6+
7+
kilos_to_pounds=lambdap:2.20462*p
8+
pounds_to_kilos=lambdak:0.453592*k
9+
pounds_to_grams=lambdap:p/1000
10+
kilos_to_grams=lambdak:k*1000
11+
meters_to_feet=lambdam:3.28084*m
12+
feet_to_meters=lambdaf:0.3048*f
13+
farenheit_to_celsius=lambdaf: (f-32.0)* (5.0/9.0)
14+
celsius_to_farenheit=lambdac: (c*9.0/5.0)+32.0
15+
16+
def__call__(cls,*args,**kwargs):
17+
cls.thismodule=cls
18+
print(cls.thismodule)
3019

31-
ifnotallowed:
32-
raiseException(f'Cannot convert{source} to{dest}.')
33-
returnTrue
20+
whitelist= {
21+
'pounds':'kilos',
22+
'kilos':'pounds',
23+
'meters':'feet',
24+
'feet':'meters',
25+
'farenheit':'celsius',
26+
'celsius':'farenheit',
27+
'volts':'gigavolts'
28+
}
29+
defis_allowed(self,source,dest):
30+
print(self.__class__.__name__)
31+
allowed= {
32+
source:self.whitelist.get(source,None)
33+
fork,vinself.whitelist.items()
34+
ifsourceinself.whitelistandself.whitelist[source]==v
35+
}
3436

35-
defadd(func,source,dest):
36-
ifis_allowed(source,dest):
37-
setattr(thismodule,'{}_to_{}'.format(source,dest),func)
37+
ifnotallowed:
38+
raiseException(f'Cannot convert{source} to{dest}.')
3839
returnTrue
39-
returnFalse
40-
41-
defconvert(source,dest,value):
42-
ifis_allowed(source,dest):
43-
val=getattr(thismodule,f'{source}_to_{dest}')(value)
44-
returnval
45-
returnNone
46-
47-
print(convert('pounds','kilos',10.0)==4.53592)
48-
print(convert('kilos','pounds',10.0)==22.0462)
49-
print(convert('kilos','grams',10.0)==10000)
50-
print(convert('pounds','grams',10.0)==4535.92)
51-
print(convert('meters','feet',10.0)==32.8084)
52-
print(convert('feet','meters',10.0)==3.048)
53-
print(round(convert('farenheit','celsius',10.0),2)==-12.22)
54-
print(convert('celsius','farenheit',10.0)==50.0)
40+
41+
defadd(self,func,source,dest):
42+
ifself.is_allowed(source,dest):
43+
setattr(Convert.thismodule,'{}_to_{}'.format(source,dest),func)
44+
returnTrue
45+
returnFalse
46+
47+
defconvert(self,source,dest,value):
48+
ifself.is_allowed(source,dest):
49+
val=getattr(Convert.thismodule,f'{source}_to_{dest}')(value)
50+
returnval
51+
returnNone
52+
53+
print(Convert().convert('pounds','kilos',10.0)==4.53592)
54+
print(Convert().convert('kilos','pounds',10.0)==22.0462)
55+
print(Convert().convert('kilos','grams',10.0)==10_000)
56+
print(Convert().convert('pounds','grams',10.0)==4535.92)
57+
print(Convert().convert('meters','feet',10.0)==32.8084)
58+
print(Convert().convert('feet','meters',10.0)==3.048)
59+
print(round(Convert().convert('farenheit','celsius',10.0),2)==-12.22)
60+
print(Convert().convert('celsius','farenheit',10.0)==50.0)
5561

5662
volts_to_gigavolts=lambdav:v*0.000000001
5763
add(volts_to_gigavolts,'volts','gigavolts')
58-
print(convert('volts','gigavolts',100))
64+
print(Convert().convert('volts','gigavolts',100))
5965

6066

6167
maxwells_to_ohms=lambdae: (e**-e**2)*e**2
6268
add(maxwells_to_ohms,'maxwells','ohms')
63-
print(convert('maxwells','ohms',120))
69+
print(Convert().convert('maxwells','ohms',120))

‎python_algorithms/exceptions.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Ensure variable is defined
2+
try:
3+
x
4+
exceptNameError:
5+
x=None
6+
7+
# Test whether variable is defined to be None
8+
ifxisNone:
9+
some_fallback_operation()
10+
else:
11+
some_operation(x)

‎python_algorithms/system/cli.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
importsys
2+
importos
3+
importthreading
4+
importrequests
5+
importtime
6+
importpprint
7+
8+
prices= {'btc':0.00 }
9+
10+
iflen(sys.argv)<2:
11+
print('please supply some coins to track')
12+
quit()
13+
14+
forcoininsys.argv[1:]:
15+
prices[coin]=0.00
16+
17+
defupdate_price(coin):
18+
whileTrue:
19+
template='https://api.bittrex.com/api/v1.1/public/getticker?market={}-{}'
20+
first_coin='usd'ifcoin=='btc'else'btc'
21+
pricing=requests.get(template.format(first_coin,coin)).json()
22+
prices[coin]=pricing['result']['Last']
23+
time.sleep(3)
24+
25+
forcoin,priceinprices.items():
26+
t=threading.Thread(target=update_price,args=(coin,))
27+
t.start()
28+
29+
whileTrue:
30+
os.system('clear')
31+
forcoin,priceinprices.items():
32+
usd_price=price*prices['btc']
33+
ifcoin=='btc':
34+
print('{} -> ${:.2f}'.format(coin.ljust(5),price))
35+
else:
36+
print('{} -> {:.8f} (${:.2f})'.format(coin.ljust(5),price,usd_price))
37+
time.sleep(.5)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
importrequests
2+
3+
r=requests.get('https://api.github.com/events')
4+
r=requests.post('https://httpbin.org/post',data= {'key':'value'})
5+
6+
r=requests.put('https://httpbin.org/put',data= {'key':'value'})
7+
r=requests.delete('https://httpbin.org/delete')
8+
r=requests.head('https://httpbin.org/get')
9+
r=requests.options('https://httpbin.org/get')
10+
11+
# passing params
12+
payload= {'key1':'value1','key2':'value2'}
13+
r=requests.get('https://httpbin.org/get',params=payload)
14+
15+
print(r.url)
16+
#=> https://httpbin.org/get?key2=value2&key1=value1
17+
# Note that any dictionary key whose value is None will not be added to the URL’s query string.
18+
19+
# You can also pass a list of items as a value:
20+
payload= {'key1':'value1','key2': ['value2','value3']}
21+
22+
r=requests.get('https://httpbin.org/get',params=payload)
23+
print(r.url)
24+
#=> https://httpbin.org/get?key1=value1&key2=value2&key2=value3
25+
26+
# CUSTOM HEADERS
27+
url='https://api.github.com/some/endpoint'
28+
headers= {'user-agent':'my-app/0.0.1'}
29+
30+
r=requests.get(url,headers=headers)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp