Locals 1

Sat 17 May 2025

title: "Locals of Function" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


def myFunc():
    print("hello")
fname = "myFunc"
f = locals()[fname]
f()
hello
f = eval(fname)
f()
hello

Score: 5

Category: basics

Read More

Logging 1

Sat 17 May 2025

title: "Logging" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


import logging

logger = logging.getLogger()
#logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
logger.setLevel(logging.DEBUG)
logging.debug("test")
DEBUG:root:test
def do_sample():
    for x in range(10):
        logging.info('one …

Category: basics

Read More

Logging 2

Sat 17 May 2025

title: "Logging 2" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


import logging

logger = logging.getLogger()
#fhandler = logging.FileHandler(filename='mylog.log', mode='a')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#fhandler.setFormatter(formatter)
logger.addHandler(fhandler)
logger.setLevel(logging.DEBUG)
def do_sample …

Category: basics

Read More

Loop-Dictionary

Sat 17 May 2025

title: "Loop Dictionary" author: "Rj" date: 2019-04-20 description: "-" type: technical_note draft: false


d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
    legs = d[animal]
    print ('A %s has %d legs' % (animal, legs))
A person has 2 legs
A cat has 4 legs
A spider has 8 legs


Score …

Category: basics

Read More

Magicattr-Sample

Sat 17 May 2025

title: "Magic Attribute Sample" author: "Rj" date: 2019-04-20 description: "-" type: technical_note draft: false


import magicattr
class City:

    def __init__(self, name, country):
        self.name = name
        self.country = country
to = City('Toronto', 'Canada')
ch = City('Chennai', 'India')
print(to)
<__main__.City object at 0x10f2f8be0>
print(magicattr.get(to, 'country'))
Canada

Score …

Category: basics

Read More

Match-All-String

Sat 17 May 2025

title: "Match All String" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


one = "Toronto is nice"
one
'Toronto is nice'
one_array = one.split(" ")
two = "It is nice to be in Toronto"
two_array = two.split(" ")
two_array
['It', 'is', 'nice', 'to', 'be', 'in', 'Toronto']
all(x in two for …

Category: basics

Read More

Modify String In Place

Sat 17 May 2025

title: "Modify String in Place" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


import io
s = "Toronto is awesome"
sio = io.StringIO(s)
print(sio.getvalue())
Toronto is awesome
sio.seek(11)
sio.write("Wonderful")
9
print(sio.getvalue())
Toronto is Wonderful


Score: 5

Category: basics

Read More

More Itertools

Sat 17 May 2025

title: "More Itertools" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


import itertools as it
import more_itertools as mit
a = it.count(0, 2)
mit.take(10, a)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

more:

https://github.com/erikrose/more-itertools



Score: 5

Category: basics

Read More

Mutable Objet

Sat 17 May 2025

title: "Mutable Object" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false source: https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument


def foo(a=[]):
    a.append(5)
    return a
foo()
[5]
foo()
[5, 5]
foo()
[5, 5, 5]

Reason:

What causes the confusion is the behaviour you get when you …

Category: basics

Read More

Name Meter

Sat 17 May 2025

title: "Get Name Meter" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


def get_int(c):
    if(c == 'a'):
        return (200 + ord(c))

    if(c == 'e'):
        return (250 + ord(c))

    if(c == 'i'):
        return (300 + ord(c))

    if(c == 'o'):
        return (350 + ord(c))

    if(c == 'u'):
        return …

Category: basics

Read More
Page 9 of 17

« Prev Next »