Why does Python have a format function as well as a format method

tldr; format just calls obj.__format__ and is used by the str.format method which does even more higher level stuff. For the lower level it makes sense to teach an object how to format itself. It is just syntactic sugar The fact that this function shares the name and format specification with str.format can be misleading. … Read more

Difference between nameof and typeof

Two reasons: nameof turns into a compile-time constant. typeof(…).Name requires a bit of reflection. It’s not overly expensive, but it can hurt in some cases. Second, it’s used for other things than type names. For example, arguments: void SomeMethod(int myArgument) { Debug.WriteLine(nameof(myArgument)); } You can also get the name of class members and even locals. … Read more

No module named builtins

I also found using ‘pip install future’ resolved this issue I got the information from here: https://askubuntu.com/questions/697226/importerror-no-module-named-builtins I hope this clarifies this for other users, like me who stumbled upon your question

Decorating Hex function to pad zeros

Starting with Python 3.6, you can: >>> value = 42 >>> padding = 6 >>> f”{value:#0{padding}x}” ‘0x002a’ for older python versions use the .format() string method: >>> “{0:#0{1}x}”.format(42,6) ‘0x002a’ Explanation: { # Format identifier 0: # first parameter # # use “0x” prefix 0 # fill with zeroes {1} # to a length of n … Read more

How to avoid SQL injection in CodeIgniter?

CodeIgniter’s Active Record methods automatically escape queries for you, to prevent sql injection. $this->db->select(‘*’)->from(‘tablename’)->where(‘var’, $val1); $this->db->get(); or $this->db->insert(‘tablename’, array(‘var1’=>$val1, ‘var2’=>$val2)); If you don’t want to use Active Records, you can use query bindings to prevent against injection. $sql=”SELECT * FROM tablename WHERE var = ?”; $this->db->query($sql, array($val1)); Or for inserting you can use the insert_string() … Read more

Override the {…} notation so i get an OrderedDict() instead of a dict()?

Here’s a hack that almost gives you the syntax you want: class _OrderedDictMaker(object): def __getitem__(self, keys): if not isinstance(keys, tuple): keys = (keys,) assert all(isinstance(key, slice) for key in keys) return OrderedDict([(k.start, k.stop) for k in keys]) ordereddict = _OrderedDictMaker() from nastyhacks import ordereddict menu = ordereddict[ “about” : “about”, “login” : “login”, ‘signup’: “signup” … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)