Python

Member

zora.stokes

Check if string is palindrome on the Python language

1
2
3
4
5
6
7
8
def ispalindrome(string):
	decide=1
	i=0
	while i<=int(len(string)/2) and decide==1:
		if string[i]!=string[-(i+1)]:
			decide=0
		i+=1
	return decide


Member

keanu.kilback

by keanu.kilback , in category: Python , 7 days ago
no answers

How to reverse string in Python 😃

1
2
3
4
def reverse(s, rs=''):
    for i in range(1, len(s)):
        rs = rs + s[len(s)-i]
    return rs+s[0]


Member

lura.oberbrunner

Nice code in Model of Django Python based framework

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# In Django model
def processed_description(self):
    text = self.description. \
           replace('<table', '<div class="table">'
                             '<div class="bgtop"></div>'
                             '<div class="overflow"><table'). \
           replace('</table>', '</table></div>'
                               '<div class="bgbottom"></div>'
                               '</div>'). \
           replace('<th', '<th width="50%" align="left"')
    return text


Member

zoie

by zoie , in category: Python , 11 days ago
no answers

How you do not need programming in Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
if cells[x-1,y-1] == 1: pos+=1
else:
  if cells[x-1,y-1] == -1: neg+=1
if cells[x-1,y] == 1: pos+=1
else:
  if cells[x-1,y] == -1: neg+=1
if cells[x-1,y+1] == 1: pos+=1
else:
  if cells[x-1,y+1] == -1: neg+=1


if cells[x,y-1] == 1: pos+=1
else:
  if cells[x,y-1] == -1: neg+=1
if cells[x,y] == 1: pos+=1
else:
  if cells[x,y] == -1: neg+=1
if cells[x,y+1] == 1: pos+=1
else:
  if cells[x,y+1] == -1: neg+=1


if cells[x+1,y-1] == 1: pos+=1
else:
  if cells[x+1,y-1] == -1: neg+=1
if cells[x+1,y] == 1: pos+=1
else:
  if cells[x+1,y] == -1: neg+=1
if cells[x+1,y+1] == 1: pos+=1
else:
  if cells[x+1,y+1] == -1: neg+=1


Member

leta

by leta , in category: Python , 12 days ago
no answers

Summer intern Python code this summer 🤗

1
2
3
4
5
6
7
8
try:
 # do some stuff
except:
  e0 = sys.exc_info()[0]
  e1 = sys.exc_info()[1]
  e2 = sys.exc_info()[2]
  print '*** failed due to: %s %s %s' % (e0, e1, e2)
  sys.exit(3)


Member

adelia_boyer

How to write detailed TODO comments in Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# TODO: fix this hell
def pretty_print(w, p):
    w = truncate(w)
    p = truncate(p)
    my_matrix = zip(p, w)
    print  "\n".join(["\t\t".join(["\t".join(map(str, r)) for r in t]) for t in my_matrix])        


# TODO: and this
def truncate(m):
    for i in range(len(m)):
        for j in range(len(m[0])):
            if(len(str(m[i][j])) > 5):
                m[i][j] = "%.3f" % m[i][j]
    return m


Member

jerald_rippin

by jerald_rippin , in category: Python , 25 days ago
no answers

Price Formatting in Python

1
2
3
4
5
6
7
def format_price price, delimeter = ' '
  s, i = price.to_s.reverse, 0
  Array.new(s.size) do |n|
    c = n.zero? ? '' : ((i += 1) % 3).zero? ? delimeter.to_s : ''
    c + s[n, 1]
  end.join.reverse
end


Member

carey

by carey , in category: Python , a month ago
no answers

Remove duplicates function in Python

1
2
3
4
def remove_duplicates(seq):
    seen = set()
    seen_add = seen.add
    return [x for x in seq if x not in seen and not seen_add(x)]


Member

carey

by carey , in category: Python , a month ago
no answers

Calculate factorial in Python 🙃

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def factorial(x):
    """ Work out x! (with a little help from the google calculator...) """

    import re
    import urllib
    import time

    time.sleep(2)

    class AppURLopener(urllib.FancyURLopener):
        def __init__(self, *args):
            # *Cough* *Cough*
            self.version = 'Mozilla 1.3'
            urllib.FancyURLopener.__init__(self, *args)

    opener = AppURLopener()
    page = opener.open('http://www.google.com/search?q=%d!' % x).read()

    result = re.findall('<b>%d ! = (.*?)</b>' % x, page)

    if result:
        return int(result[0].replace('<font size=-2> </font>', ''))
    else:
        raise Exception, "Google not willing today!:\n\n %s" % page


Member

carey

by carey , in category: Python , a month ago
no answers

Validate Line function in Python 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def validate_line(line):
    if len(line) <= 2: return False
    if line[0] == '""': return False
    if line[0] == "''": return False
    if line[0] == '+!': return False
    if line[0] == '-?': return False
    if line[0] == '- (': return False
    if line[0] == '-(': return False
    if line[0] == '-)': return False

    if line[0] == '-6a': return False
    if line[0] == '-6a )': return False
    if line[0] == '-a': return False
    if line[0] == '-aa': return False

    if line[0] == '-:': return False
    if line[0] == ':': return False
    if line[0][0] == '#': return False
    if line[0][0] == "'": return False
    if line[0][0] == '&': return False
    if line[0][0] == '(': return False
    return True