Search code snippets, questions, articles... Chrome Extension

python code snippets

Pandas dataframe to dictionary conversion python
import pandas as pd

data = {
  'name': ['Rick', 'Carol', 'Carl', 'Negan'],
  'place': ['Alexendria', 'Kingdom', 'Alexendria', 'Saviours']
} 
df = pd.DataFrame.from_dict(data)
print(df)
#     name      place
# 0   Rick      Alexendria
# 1   Carol     Kingdom
# 2   Carl      Alexendria
# 3   Negan     Saviours

# Get dict from dataframe(Method 1)
dict1 = dict(df.values)
print(dict1)
# -> {'Rick': 'Alexendria', 'Carol': 'Kingdom', 'Carl': 'Alexendria', 'Negan': 'Saviours'}

# Get dict from dataframe(Method 2)
dict2 = df.to_dict('index')
print(dict2)
# -> {0: {'name': 'Rick', 'place': 'Alexendria'}, 1: {'name': 'Carol', 'place': 'Kingdom'}, 2: {'name': 'Carl', 'place': 'Alexendria'}, 3: {'name': 'Negan', 'place': 'Saviours'}}
Python check NaN values with and without using packages
# By comparing itself
def is_nan(val):
  return val != val

nan_value = float("nan")
print(is_nan(nan_value))
# -> True

#Using math.isnan(Import math before using math.isnan)
import math

nan_value = float("NaN")
check_nan = math.isnan(nan_value)
print(check_nan)
# -> True
String formation and interpolation using Python
str_1 = "Hello"
str_2 = "Devsheet"

#Using .format() method
#Using blank curly brackets
new_str_1 = "Welcome User {} from {}".format(str_1, str_2)
print(new_str_1)
# -> Welcome User Hello from Devsheet

#Using variable names
new_str_2 = "{s1} from {s2}.".format(s1=str_1, s2=str_2)
print(new_str_2)
# -> Hello from Devsheet.
Python code to remove falsy values(None, False, [], {}, etc)
def sanatize_list(lst):
  return list(filter(bool,lst))

list_eg_1 = ["Devsheet", None, "", False]
eg_1_result = sanatize_list(list_eg_1)
print(eg_1_result)
# -> ['Devsheet']

list_eg_2 = [None, "John", False, "Deo"]
eg_2_result = sanatize_list(list_eg_2)
print(eg_2_result)
# -> ['John', 'Deo']

list_eg_3 = [[], "Apple", (), {}]
eg_3_result = sanatize_list(list_eg_3)
print(eg_3_result)
Filter by current time - n hours SQLAlchemy
import datetime, timedelta

n = 10
time_difference = datetime.now() - timedelta(hours=n)
result = session.query(User).filter(
            User.last_active_timestamp < time_difference
        )
Print whole month from calendar using python
import calendar

select_year = 2021
select_month = 11

print(calendar.month(select_year, select_month))

#    November 2021
# Mo Tu We Th Fr Sa Su
#  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
Get highest id or value record from a column SqlAlchemy Query
from sqlalchemy import func 

session.query(func.max(table_name.column_name))

#To get all records
session.query(func.max(table_name.column_name)).all()

#To get first record
session.query(func.max(table_name.column_name)).first()
Celery start command
$ celery -A <project_name> worker -E -l INFO
$ celery -A <project_name> beat -E -l INFO
Validate password with and without regex in Python
#without using regex
def validate_password(password):
  special_chars =['$', '!', '@', '#', '%', '&']
  validated = True
  msg = ''

  if len(password) < 8:
    msg = 'Password length must be at least 8'
    validated = False
    
  elif len(password) > 18:
    msg = 'Password length must not be greater than 18'
    validated = False
    
  elif not any(char.isdigit() for char in password):
    msg = 'Password should have at least one number'
    validated = False

  elif not any(char.isupper() for char in password):
    msg = 'Password should have at least one uppercase letter'
    validated = False

  elif not any(char.islower() for char in password):
    msg = 'Password should have at least one lowercase letter'
    validated = False
        
  elif not any(char in special_chars for char in password):
    msg = 'Password should have at least one special character'
    validated = False
    
  return { 'is_valid': validated, 'message': msg }

print(validate_password('Hellowewew1@'))
Python code examples to flatten a nested dictionary
#Using pandas DataFrame
import pandas as pd

d = {'x': 1,
     'y': {'a': 2, 'b': {'c': 3, 'd' : 4}},
     'z': {'a': 2, 'b': {'c': 3, 'd' : 4}}}

dframe = pd.json_normalize(d, sep='_')

print(dframe.to_dict(orient='records')[0])
Change the name of columns in a pandas dataframe
#By creating a copy - using df.rename() method
df = df.rename(columns={'col_1': 'changed_col_1', 'col_2': 'changed_col_2'})
print(df)

#Without creating copy - using df.rename() method with inplace=True
df.rename(columns={'col_1': 'changed_col_1', 'col_2': 'changed_col_2'}, inplace=True)
print(df)
String characters to list items using python
my_str = "Devsheet"

my_list = list(my_str)
print(my_list)

#-> ['D', 'e', 'v', 's', 'h', 'e', 'e', 't']
Check if given value is a number or integer in python
#By throwing error
def check_int(value):  
  try:
    value_int = int(value)
    return 'Value is integer'
  except ValueError:
    return 'Not an integer'

check_int_response = check_int('20')
print(check_int_response)

# using isdigit() method - only for string format and positive values
value = "20"
if value.isdigit():
  print("value is integer")
else:
  print("value is not integer")
flat_dict.py
"""FlatDict is a dict object that allows for single level, delimited
key/value pair mapping of nested dictionaries.

"""
from collections.abc import MutableMapping

NO_DEFAULT = object()


class FlatDict(MutableMapping):
    """:class:`~flatdict.FlatDict` is a dictionary object that allows for
    single level, delimited key/value pair mapping of nested dictionaries.
    The default delimiter value is ``:`` but can be changed in the constructor
    or by calling :meth:`FlatDict.set_delimiter`.

    """

    _COERCE = dict

    def __init__(self, value=None, delimiter=":", dict_class=dict):
        super(FlatDict, self).__init__()
        self._values = dict_class()
        self._delimiter = delimiter
        self.update(value)

    def __contains__(self, key):
        """Check to see if the key exists, checking for both delimited and
        not delimited key values.

        :param mixed key: The key to check for

        """
        if self._has_delimiter(key):
            pk, ck = key.split(self._delimiter, 1)
            return pk in self._values and ck in self._values[pk]
        return key in self._values

    def __delitem__(self, key):
        """Delete the item for the specified key, automatically dealing with
        nested children.

        :param mixed key: The key to use
        :raises: KeyError

        """
        if key not in self:
            raise KeyError
        if self._has_delimiter(key):
            pk, ck = key.split(self._delimiter, 1)
            del self._values[pk][ck]
            if not self._values[pk]:
                del self._values[pk]
        else:
            del self._values[key]

    def __eq__(self, other):
        """Check for equality against the other value

        :param other: The value to compare
        :type other: FlatDict
        :rtype: bool
        :raises: TypeError

        """
        if isinstance(other, dict):
            return self.as_dict() == other
        elif not isinstance(other, self.__class__):
            raise TypeError
        return self.as_dict() == other.as_dict()

    def __ne__(self, other):
        """Check for inequality against the other value

        :param other: The value to compare
        :type other: dict or FlatDict
        :rtype: bool

        """
        return not self.__eq__(other)

    def __getitem__(self, key):
        """Get an item for the specified key, automatically dealing with
        nested children.

        :param mixed key: The key to use
        :rtype: mixed
        :raises: KeyError

        """
        values = self._values
        key = [key] if isinstance(key, int) else key.split(self._delimiter)
        for part in key:
            values = values[part]
        return values

    def __iter__(self):
        """Iterate over the flat dictionary key and values

        :rtype: Iterator
        :raises: RuntimeError

        """
        return iter(self.keys())

    def __len__(self):
        """Return the number of items.

        :rtype: int

        """
        return len(self.keys())

    def __reduce__(self):
        """Return state information for pickling

        :rtype: tuple

        """
        return type(self), (self.as_dict(), self._delimiter)

    def __repr__(self):
        """Return the string representation of the instance.

        :rtype: str

        """
        return '<{} id={} {}>"'.format(self.__class__.__name__, id(self), str(self))

    def __setitem__(self, key, value):
        """Assign the value to the key, dynamically building nested
        FlatDict items where appropriate.

        :param mixed key: The key for the item
        :param mixed value: The value for the item
        :raises: TypeError

        """
        if isinstance(value, self._COERCE) and not isinstance(value, FlatDict):
            value = self.__class__(value, self._delimiter)
        if self._has_delimiter(key):
            pk, ck = key.split(self._delimiter, 1)
            if pk not in self._values:
                self._values[pk] = self.__class__({ck: value}, self._delimiter)
                return
            elif not isinstance(self._values[pk], FlatDict):
                raise TypeError("Assignment to invalid type for key {}".format(pk))
            self._values[pk][ck] = value
        else:
            self._values[key] = value

    def __str__(self):
        """Return the string value of the instance.

        :rtype: str

        """
        return "{{{}}}".format(
            ", ".join(["{!r}: {!r}".format(k, self[k]) for k in self.keys()])
        )

    def as_dict(self):
        """Return the :class:`~flatdict.FlatDict` as a :class:`dict`

        :rtype: dict

        """
        out = dict({})
        for key in self.keys():
            if self._has_delimiter(key):
                pk, ck = key.split(self._delimiter, 1)
                if self._has_delimiter(ck):
                    ck = ck.split(self._delimiter, 1)[0]
                if isinstance(self._values[pk], FlatDict) and pk not in out:
                    out[pk] = {}
                if isinstance(self._values[pk][ck], FlatDict):
                    out[pk][ck] = self._values[pk][ck].as_dict()
                else:
                    out[pk][ck] = self._values[pk][ck]
            else:
                out[key] = self._values[key]
        return out

    def clear(self):
        """Remove all items from the flat dictionary."""
        self._values.clear()

    def copy(self):
        """Return a shallow copy of the flat dictionary.

        :rtype: flatdict.FlatDict

        """
        return self.__class__(self.as_dict(), delimiter=self._delimiter)

    def get(self, key, d=None):
        """Return the value for key if key is in the flat dictionary, else
        default. If default is not given, it defaults to ``None``, so that this
        method never raises :exc:`KeyError`.

        :param mixed key: The key to get
        :param mixed d: The default value
        :rtype: mixed

        """
        try:
            return self.__getitem__(key)
        except KeyError:
            return d

    def items(self):
        """Return a copy of the flat dictionary's list of ``(key, value)``
        pairs.

        .. note:: CPython implementation detail: Keys and values are listed in
            an arbitrary order which is non-random, varies across Python
            implementations, and depends on the flat dictionary's history of
            insertions and deletions.

        :rtype: list

        """
        return [(k, self.__getitem__(k)) for k in self.keys()]

    def iteritems(self):
        """Return an iterator over the flat dictionary's (key, value) pairs.
        See the note for :meth:`flatdict.FlatDict.items`.

        Using ``iteritems()`` while adding or deleting entries in the flat
        dictionary may raise :exc:`RuntimeError` or fail to iterate over all
        entries.

        :rtype: Iterator
        :raises: RuntimeError

        """
        for item in self.items():
            yield item

    def iterkeys(self):
        """Iterate over the flat dictionary's keys. See the note for
        :meth:`flatdict.FlatDict.items`.

        Using ``iterkeys()`` while adding or deleting entries in the flat
        dictionary may raise :exc:`RuntimeError` or fail to iterate over all
        entries.

        :rtype: Iterator
        :raises: RuntimeError

        """
        for key in self.keys():
            yield key

    def itervalues(self):
        """Return an iterator over the flat dictionary's values. See the note
        :meth:`flatdict.FlatDict.items`.

        Using ``itervalues()`` while adding or deleting entries in the flat
        dictionary may raise a :exc:`RuntimeError` or fail to iterate over all
        entries.

        :rtype: Iterator
        :raises: RuntimeError

        """
        for value in self.values():
            yield value

    def keys(self):
        """Return a copy of the flat dictionary's list of keys.
        See the note for :meth:`flatdict.FlatDict.items`.

        :rtype: list

        """
        keys = []

        for key, value in self._values.items():
            if isinstance(value, (FlatDict, dict)):
                nested = [
                    self._delimiter.join([str(key), str(k)]) for k in value.keys()
                ]
                keys += nested if nested else [key]
            else:
                keys.append(key)

        return keys

    def pop(self, key, default=NO_DEFAULT):
        """If key is in the flat dictionary, remove it and return its value,
        else return default. If default is not given and key is not in the
        dictionary, :exc:`KeyError` is raised.

        :param mixed key: The key name
        :param mixed default: The default value
        :rtype: mixed

        """
        if key not in self and default != NO_DEFAULT:
            return default
        value = self[key]
        self.__delitem__(key)
        return value

    def setdefault(self, key, default):
        """If key is in the flat dictionary, return its value. If not,
        insert key with a value of default and return default.
        default defaults to ``None``.

        :param mixed key: The key name
        :param mixed default: The default value
        :rtype: mixed

        """
        if key not in self:
            self.__setitem__(key, default)
        return self.__getitem__(key)

    def set_delimiter(self, delimiter):
        """Override the default or passed in delimiter with a new value. If
        the requested delimiter already exists in a key, a :exc:`ValueError`
        will be raised.

        :param str delimiter: The delimiter to use
        :raises: ValueError

        """
        for key in self.keys():
            if delimiter in key:
                raise ValueError(
                    "Key {!r} collides with delimiter {!r}", key, delimiter
                )
        self._delimiter = delimiter
        for key in self._values.keys():
            if isinstance(self._values[key], FlatDict):
                self._values[key].set_delimiter(delimiter)

    def update(self, other=None, **kwargs):
        """Update the flat dictionary with the key/value pairs from other,
        overwriting existing keys.

        ``update()`` accepts either another flat dictionary object or an
        iterable of key/value pairs (as tuples or other iterables of length
        two). If keyword arguments are specified, the flat dictionary is then
        updated with those key/value pairs: ``d.update(red=1, blue=2)``.

        :param iterable other: Iterable of key, value pairs
        :rtype: None

        """
        [self.__setitem__(k, v) for k, v in dict(other or kwargs).items()]

    def values(self):
        """Return a copy of the flat dictionary's list of values. See the note
        for :meth:`flatdict.FlatDict.items`.

        :rtype: list

        """
        return [self.__getitem__(k) for k in self.keys()]

    def _has_delimiter(self, key):
        """Checks to see if the key contains the delimiter.

        :rtype: bool

        """
        return isinstance(key, str) and self._delimiter in key


class FlatterDict(FlatDict):
    """Like :class:`~flatdict.FlatDict` but also coerces lists and sets
    to child-dict instances with the offset as the key. Alternative to
    the implementation added in v1.2 of FlatDict.

    """

    _COERCE = list, tuple, set, dict, FlatDict
    _ARRAYS = list, set, tuple

    def __init__(self, value=None, delimiter=":", dict_class=dict):
        self.original_type = type(value)
        if self.original_type in self._ARRAYS:
            value = {str(i): v for i, v in enumerate(value)}
        super(FlatterDict, self).__init__(value, delimiter, dict_class)

    def __setitem__(self, key, value):
        """Assign the value to the key, dynamically building nested
        FlatDict items where appropriate.

        :param mixed key: The key for the item
        :param mixed value: The value for the item
        :raises: TypeError

        """
        if isinstance(value, self._COERCE) and not isinstance(value, FlatterDict):
            value = self.__class__(value, self._delimiter)
        if self._has_delimiter(key):
            pk, ck = key.split(self._delimiter, 1)
            if pk not in self._values:
                self._values[pk] = self.__class__({ck: value}, self._delimiter)
                return
            if getattr(self._values[pk], "original_type", None) in self._ARRAYS:
                try:
                    k, cck = ck.split(self._delimiter, 1)
                    int(k)
                except ValueError:
                    raise TypeError(
                        "Assignment to invalid type for key {}{}{}".format(
                            pk, self._delimiter, ck
                        )
                    )
                self._values[pk][k][cck] = value
                return
            elif not isinstance(self._values[pk], FlatterDict):
                raise TypeError("Assignment to invalid type for key {}".format(pk))
            self._values[pk][ck] = value
        else:
            self._values[key] = value

    def as_dict(self):
        """Return the :class:`~flatdict.FlatterDict` as a nested
        :class:`dict`.

        :rtype: dict

        """
        out = {}
        for key in self.keys():
            if self._has_delimiter(key):
                pk, ck = key.split(self._delimiter, 1)
                if self._has_delimiter(ck):
                    ck = ck.split(self._delimiter, 1)[0]
                if isinstance(self._values[pk], FlatterDict) and pk not in out:
                    if self._values[pk].original_type == tuple:
                        out[pk] = tuple(self._child_as_list(pk))
                    elif self._values[pk].original_type == list:
                        out[pk] = self._child_as_list(pk)
                    elif self._values[pk].original_type == set:
                        out[pk] = set(self._child_as_list(pk))
                    elif self._values[pk].original_type == dict:
                        out[pk] = self._values[pk].as_dict()
            else:
                if isinstance(self._values[key], FlatterDict):
                    out[key] = self._values[key].original_type()
                else:
                    out[key] = self._values[key]
        return out

    def _child_as_list(self, pk, ck=None):
        """Returns a list of values from the child FlatterDict instance
        with string based integer keys.

        :param str pk: The parent key
        :param str ck: The child key, optional
        :rtype: list

        """
        if ck is None:
            subset = self._values[pk]
        else:
            subset = self._values[pk][ck]
        # Check if keys has delimiter, which implies deeply nested dict
        keys = subset.keys
        if any(self._has_delimiter(k) for k in keys):
            out = []
            split_keys = {k.split(self._delimiter)[0] for k in keys}
            for k in sorted(split_keys, key=lambda x: int(x)):
                if subset[k].original_type == tuple:
                    out.append(tuple(self._child_as_list(pk, k)))
                elif subset[k].original_type == list:
                    out.append(self._child_as_list(pk, k))
                elif subset[k].original_type == set:
                    out.append(set(self._child_as_list(pk, k)))
                elif subset[k].original_type == dict:
                    out.append(subset[k].as_dict())
            return out

        return [subset[k] for k in keys]
Python program to return multiple values from a function
def my_function():
    value_1 = "devsheet"
    value_2 = 100
    value_3 = ["item 1", "item 2"]
    return value_1, value_2, value_3;

value_1, value_2, value_3 = my_function()
print(value_1)
print(value_2)
print(value_3)
Get exception text in python
try:
  result = 3/0 #this will throw error
  
except Exception as e:
  print("Repr : " + repr(e)) # Use repr to get error
  print("Error : " + str(e)) # Use str to get error
Use of try, except, else and finally in python
try:
  some_undefined_method()
  
except Exception as e:
  print("Repr Error : " + repr(e))
  print("Error : " + str(e))
  
else:
  print("Program Executed successfully")
  
finally:
  print("Finally always executes")
Use of else condition with for loop in Python
#Using else with for loop
for i in range(4):
    print(i)
else:  # Will execute after loop completed if no break statement
    print("I am in else")

#else will not be executed if 'break' executed used inside loop
for item in ['one', 'two', 'three']:
    if item == 'two':
        break
    print(item)
else:
    print("This will not be executed")
Calculate the factorial of a number using python code
def factorial(x):
    if x == 1:
        return 1
    else:
        return (x * factorial(x-1))

num = 4
print("The factorial of", num, "is", factorial(num))
Python code to validate email address using regex
import re

def validate_email(email):
  email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
  if ( re.fullmatch(email_pattern, email) ):
    print("Email is Valid")
  else:
    print("Email is Invalid")

validate_email("test@domain.com")
# -> prints - Email is Valid

validate_email("testdomain.com")
# -> prints - Email is Invalid
Django bulk get or create
IntegrationRequirement.objects.bulk_create(objs=objs, ignore_conflicts=True)
Trim or remove begin and end spaces from a python string
my_str = "  This string contains spaces.  "

#trim python string using strip() method
trimmmed_str = my_str.strip()
print(trimmmed_str)

#remove leading(beginning) whitespace
lstr = my_str.lstrip()

#remove trailing(end) whitespace
rstr = my_str.rstrip()
get_or_create()
try:
    obj = Person.objects.get(first_name='John', last_name='Lennon')
except Person.DoesNotExist:
    obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
    obj.save()

#shortcut for the above code

obj, created = Person.objects.get_or_create(
    first_name='John',
    last_name='Lennon',
    defaults={'birthday': date(1940, 10, 9)},
)
Get the index of the list inside python for loop
fruits_list = ["Apple", "Orange", "Banana", "Pineapple"]

#using enumerate() method
for index, value in enumerate(fruits_list):
    print(index, value)

#using range() method
for index in range(len(fruits_list)):
    print(index, fruits_list[index])
Reverse a string or number in python
#reverse a number
my_num = 12345
reversed_num = str(my_num)[::-1]
print(reversed_num)
# -> 54321

#reverse a string
my_str = "hello"
reversed_str = my_str[::-1]
print(reversed_str)
# -> olleh
Get last item or value from a List in python
subjects = ["Math", "Physics", "Chemistry", "English"]

last_item = subjects[-1]
print(last_item)

# prints - English
Delete a key value pair from a dictionary in Python
my_car = {
  "name": "Nexon",
  "brand": "TATA",
  "color": "Green"
}

del my_car['color']

print(my_car)
Checking if List is empty in Python with Examples
fruit_list = []

# Using not keyword
if not fruit_list:
    print("List is empty")

# Using len() method
if len(fruit_list) == 0:
    print("List is empty")
Python3 program to iterate over a list
my_list = ["one", "two", "three", "four", "five"]

# Using for loop
for item in my_list:
    print(item)

# Using range by getting list length
for i in range(len(my_list)):
    print(my_list[i])
Apply condition based multiple filters in SQLAlchemy query
all_filters = [UserModal.role == 'admin']

if user.last_name:
    all_filters.append(UserModal.last_name == 'Deo')

db.session.query(
    UserModal.username
).filter(
    **all_filters
).all()
Merge two or multiple dictionaries in Python
user_info = {
  "name": "John",
  "email": "test@test.com"
}

address_info = {
  "city": "New York",
  "country": "USA"
}

#Merge using .update() method
user_info.update(address_info)
print(user_info);

#Merge using ** operator
merged = {**user_info, **address_info}
print(merged)
Django update model with for loop
def update(self, instance, validated_data):
    for attr, value in validated_data.items():
        setattr(instance, attr, value)
    instance.save()
    return instance
SQLAlchemy query to get distinct records from table
db.session.query(
    UserModel.city
).distinct().all()
Django send post request with different field than primary key.
class ContactCompanySerializer(serializers.ModelSerializer):
    # profile=ProfileSerializer()
    # array şeklinde gönderiyoruz. Array içindekiler de charfield yani string.
    integration_requirements = serializers.ListSerializer(
        child=serializers.CharField())

    class Meta:
        model = ContactCompany
        fields = ('id', 'name', 'website', 'country', 'city',
                  'address', 'sector', 'note', 'profile',
                  'integration_requirements')
        # exclude_fields = ('created_at', 'updated_at', 'is_active')
    
    # create methodunu override ediyoruz.
    def create(self, validated_data):
        # integration requirements kısmını validatet datadan çekiyoruz (array olarak).
        integration_requirements = validated_data.pop(
            'integration_requirements', [])
        # company objemizi oluÅŸturuyoruz ve IntegrationRequirement modelinden
        # post requestte gönderdiğimiz requirementleri filtreliyoruz.
        company = super().create(validated_data)
        integration_requirements_qs = IntegrationRequirement.objects.filter(
            requirement__in=integration_requirements)
        # Company'e direkt olarak requirementsleri ekliyoruz.
        company.integration_requirements.add(*integration_requirements_qs)
        return company
Django Rest Framework field level serializer validation.
from rest_framework import serializers

class BlogPostSerializer(serializers.Serializer):
    title = serializers.CharField(max_length=100)
    content = serializers.CharField()

    def validate_title(self, value):
        """
        Check that the blog post is about Django.
        """
        if 'django' not in value.lower():
            raise serializers.ValidationError("Blog post is not about Django")
        return value
Convert comma separated string values to tuple python
fruits = "Apple,Mango,Orange"
fruit_list = fruits.split(',')
print( tuple(fruit_list) )
# -> ('Apple', 'Mango', 'Orange')
Use of IN Clause Sqlalchemy
session.query(
    MyModelname
).filter(
    MyModelname.id.in_((10,11,12,13))
).all()
delete data from table
# Packages

import os
from google.cloud import bigquery
from google.oauth2 import service_account


# Parameters
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]='/Users/jmbenedetto/Library/Mobile Documents/com~apple~CloudDocs/#Formação/Dissertation/Research/02 Research v2/00 Base/key.json'

client = bigquery.Client(project=project_name)
dataset_ref = client.dataset(dataset_name)
load_config = bigquery.LoadJobConfig()


# Code

query = """
        DELETE 
        FROM dataset_name.table_name
        WHERE criteria;
"""
query_params = [
    bigquery.ArrayQueryParameter("file", "STRING", file_source)
]

job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(
    query,
    # Location must match that of the dataset(s) referenced in the query.
    location="EU",
    job_config=job_config,
)  # API request - starts the query

query_job.result(timeout=60.0)
assert query_job.state == "DONE"
update data from table
query="""
    UPDATE datalab.jmb_deliveries
    SET ot_client_order_ind = CASE
        WHEN delivery_date>promise_date THEN 0
        ELSE 1
        END
    WHERE
        client_order_ind = 1
    """
query_job = client.query(
        query,
        # Location must match that of the dataset(s) referenced in the query.
        location="EU"
        )
query_job.result(timeout=120)
testPASSMON
print(1)
Convert Python Collections - List, Dict to JSON String
import json

my_dict = {
  "id": 1,
  "name": "Robert Downey Jr.",
  "nick_name": "Iron Man",
  "city": "New York"
}

json_str = json.dumps(my_dict)
print(json_str)
# -> "{"id": 1, "name": "Robert Downey Jr.", "nick_name": "Iron Man", "city": "New York"}"
Convert JSON string to Python collections - like list, dictionaries
import json

#Convert object to python dict
json_str = '{"id": 1, "name": "Suman Kumar", "city": "Delhi"}'
my_dict = json.loads(json_str)
print(my_dict["id"])
print(my_dict["name"])
print(my_dict["city"])
# -> 1
# -> Suman Kumar
# -> Delhi

#Convert to python list of dictionaries
json_str = """[
  {"id": 1, "name": "Suman Kumar"},
  {"id": 2, "name": "Gaurav"},
  {"id": 3, "name": "John"}
]"""
my_list = json.loads(json_str)
print(my_list[1])
# -> prints {'id': 2, 'name': 'Gaurav'}
Calculate the square root of a number in python
import math

#First Method - No need to import math module
number = 8
sq_root = number ** 0.5
print(sqrt)

#Second Method - Using math.sqrt() method
sq_root = math.sqrt(number)
print(sq_root)

#Third Method - Using math.pow() method
sq_root = math.pow(number, 0.5)
print(sq_root)
Random numbers List with no repetition - generate in python
import random

final_list = random.sample(range(70), 15)
print(final_list)
How to generate a random number in python using random module
import random

print (random.randint(1,50))
#It will print a random number between 1 to 50

print (random.random())
#It will print a Float random number between 0 to 1

print(random.sample( range(5, 24), 7) )
# Prints a list of 7 random numbers between 5 to 24
Get column names from Pandas DataFrame as a python List
import pandas as pd

df = pd.DataFrame({
    'Name': ['John', 'Eric', 'Rick'],
    'Score': [200, 500, 100\]
})

df_columns = df.columns.values.tolist()
print(df_columns)
#prints - ['Name', 'Score']
Get a value from DataFrame row using index and column name in pandas
import pandas as pd

df = pd.DataFrame([[0, 2, 3], [0, 4, 1], [10, 20, 30]],
                  index=[1, 2, 3], columns=['A', 'B', 'C'])

#index - 3, column_name - 'C'
value = df.at[3, 'C']
print(value) # prints - 30

#this can also written as
value = df.loc[3].at['C']
print( value ) # prints - 30
Loop through DataFrame rows in python pandas
import pandas as pd

df = pd.DataFrame({
    'column_1': ['John', 'Eric', 'Rick'],
    'column_2': [100, 110, 120]
})

for index, row in df.iterrows():
    print(row['column_1'], row['column_2'])

#prints
# John 100
# Eric 110
# Rick 120
Create pandas DataFrame and add columns and rows to it
import pandas as pd
  
#first method - create empty dataframe
df = pd.DataFrame()

# append columns and rows to this dataframe
df['username'] = ['john', 'neil', 'curtis']
df['status'] = ['active', 'disabled', 'active']
print(df)

#----------------------------------------------------------------------------------#

#second method - create dataframe with columns
df = pd.DataFrame(columns = ['username', 'status'])

# append rows to the abo
df = df.append({'username' : 'john', 'status' : 'active'}, ignore_index = True)
df = df.append({'username' : 'neil', 'status' : 'disabled'}, ignore_index = True)
df = df.append({'username' : 'curtis', 'status' : 'active'}, ignore_index = True)
print(df)

#----------------------------------------------------------------------------------#

#make data frame with rows having - NaN values at index - a, b and c
df = pd.DataFrame(columns = ['username', 'status'], index = ['a', 'b', 'c'])
print(df)
#  +---+----------+--------+
#  |   | username | status |
#  +---+----------+--------+
#  | a |      NaN |    NaN |
#  | b |      NaN |    NaN |
#  | c |      NaN |    NaN |
#  +---+----------+--------+

# add rows at already created indexes
df.loc['a'] = ['john', 'active']
df.loc['b'] = ['neil', 'disabled']
df.loc['c'] = ['curtis', 'active']
print(df)
Convert a dictionary to pandas DataFrame in python
import pandas as pd

my_dict = [{'Name': 'John', 'Score': 20}, {'Name': 'Mark', 'Score': 50}, {'Name': 'Rick', 'Score': 70}]

df = pd.DataFrame.from_dict(my_dict)
print(df)

#   +----+--------+---------+
#   |    |  Name  |  Score  |
#   +-------------+---------+
#   | 0  |  John  |     20  |
#   | 1  |  Mark  |     50  |
#   | 2  |  Rick  |     70  |
#   +----+--------+---------+
Assign multi-line string to a variable in python
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
Convert all string characters to lowercase in python
my_str = "Hello World!"
print( my_str.lower() )
#prints - hello world
Convert pandas DataFrame to List of dictionaries python
import pandas as pd

df_fruits = pd.DataFrame()
df_fruits["fruit_name"] = ["Orange", "Banana", "Apple"]
df_fruits["color"] = ["orange", "yellow", "red"]

print(df_fruits.to_dict('records'))
# prints - [{'fruit_name': 'Orange', 'color': 'orange'}, {'fruit_name': 'Banana', 'color': 'yellow'}, {'fruit_name': 'Apple', 'color': 'red'}]
Convert pandas DataFrame to python collection - dictionary
import pandas as pd

df = pd.DataFrame()
df["Name"] = ["John", "Mark", "Rick"]
df["Score"] = [20, 50, 70]

print(df.to_dict())
# prints - {'Name': {0: 'John', 1: 'Mark', 2: 'Rick'}, 'Score': {0: 20, 1: 50, 2: 70}}

print(df.to_dict('split'))
# prints - {'index': [0, 1, 2], 'columns': ['Name', 'Score'], 'data': [['John', 20], ['Mark', 50], ['Rick', 70]]}

print(df.to_dict('records'))
# prints - [{'Name': 'John', 'Score': 20}, {'Name': 'Mark', 'Score': 50}, {'Name': 'Rick', 'Score': 70}]

print(df.to_dict('index'))
#prints - {0: {'Name': 'John', 'Score': 20}, 1: {'Name': 'Mark', 'Score': 50}, 2: {'Name': 'Rick', 'Score': 70}}
Get all values by key name from a list of dictionaries Python
users = [
    { "name": "John Deo", "username": "john01" },
    { "name": "Stephen Hawk", "username": "stephen" },
    { "name": "Rick Grimes", "username": "rick21" }
]

names = [d["name"] for d in fruits] #returns list
print(names)
#prints - ["John Deo", "Stephen Hawk", "Rick Grimes"]


names_comma_seperated = ','.join( [d["title"] for d in data['current']] )
print(names_comma_seperated)
#prints - John Deo,Stephen Hawk,Rick Grimes
Check if a key exist in a dictionary Python
#dictionary - user
user = {
    "name": "John",
    "email": "mail@domain.com"
}

#check using has_key
if user.has_key('email'):
    print("email key exists in the dictionary")
else:
    print("email key does not exist")

#check using in
if 'email' in user:
  print "email key exists"
else:
  print "email key does not exist"
Send post request with data and headers Python requests
import requests

raw_json_data = { "user_name": "hello", "password": "password1" }
data = { "first_name": "hello", "last_name": "world" }
headers = { 'Authorization': 'Bearer token_value_here' }

response = requests.post('https://path/to/api/url/endpoint', headers = headers, data = data, json=raw_json_data)

print(response.status) #prints the status of request
print(response.content) #prints the content
print(response.json()) #json formatted data if response returns any
Get User input from terminal in Python
user_input = input("MESSAGE YOU WANT TO SHOW : ")

print(user_input)
Install Pandas using pip Python
pip install pandas
model api
# load all models

import os
curr_dir = os.path.abspath('.')
os.chdir('/home/jupyter/model-api/')

try:
    import networks as main
    model = main.Networks()
finally:
    os.chdir(curr_dir)
Using and on filter query SQLAlchemy
from sqlalchemy import and_

session.query(
    UserModel.id,
    UserModel.first_name
).filter(
    and_(
        UserModel.first_name == 'john', 
        UserModel.last_name == 'michael'
    )
).all()
Loop through string characters in Python
for char in "orange":
    print(char)
Get string length using len() method python
my_str = "The world is beautiful"
print(len(my_str)) #prints 22
Get character from a string using index Python
my_str = "We are here to help"
print(my_str[4]) #prints 'r'
print(my_str[7]) #prints 'h'
Count Rows group by column name SqlAlchemy
user_counts = session.query(
                UserModel.role, 
                func.count(UserModel.role).label("total_counts")
            ).group_by(
                UserModel.role
            ).all()

#THE ABOVE QUERY CAN OUTPUT DATA:
#  +-------------+----------------+
#  | role        | total_counts   |
#  +-------------+----------------+
#  | Admin       | 100            |
#  | Super Admin | 50             |
#  | user        | 1700           |
#  +-------------+----------------+
Create table model using SQLAlchemy
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String
from datetime import datetime

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String, nullable=False)
    firstname = Column(String)
    lastname = Column(String, nullable=True, default='')
    created_date = db.Column(db.DateTime, default=datetime.now())

    def __repr__(self):
        return "<User(username='%s', firstname='%s', lastname='%s')>" % (self.username, self.firstname, self.lastname)
Implement pagination in Flask-SQLAlchemy
employees = EmployeeModel\
            .query\
            .order_by(EmployeeModel.id.desc())\
            .paginate(
                page=1,
                per_page=10,
                max_per_page=10,
                error_out=False
            )

result = {
    "total_records": employees.total,
    "page": employees.page,
    "items": employees.items
}
Add column to existing table with default value SQLAlchemy
from sqlalchemy import text

class User_Model(db.Model):
    #use server_default if table already created to add default value
    flag = db.Column(db.Boolean, nullable=False, server_default=text('1'))
Replace NULL with false while Selecting in SQLAlchemy
from sqlalchemy import func

# SYNTAX - func.coalesce(model.prop, default_value)
db.session.query(
    UserModel.id,
    func.coalesce(UserModel.active, false).label('is_active'),
).filter(
    UserModel.status == 'active'
).all()
Filter based on NULL Values in SQLAlchemy
from sqlalchemy import or_

dbsession.query(
    EmployeeModel
).filter(
    or_(
        EmployeeModel.status != 'active',
        EmployeeModel.status == None #this is used to check NULL values
    )
).all()
Apply join based on multiple conditions SQLAlchemy
from sqlalchemy import or_, and_

#USING AND CONDITION
session.query(
    EmployeeModel.name,
    EmployeeDepartment.dept_name
).join(
    EmployeeDepartment,
    and_(
        EmployeeDepartment.employee_id == EmployeeModel.id, 
        EmployeeDepartment.dept_code == 'P01'
    )
).all()


#USING OR CONDITION
session.query(
    EmployeeModel.name,
    EmployeeDepartment.dept_name
).join(
    EmployeeDepartment,
    or_(
        EmployeeDepartment.employee_id == EmployeeModel.id, 
        EmployeeDepartment.dept_code == 'P01'
    )
).all()
Applying Left Outer Join query in SQLAlchemy
self.session.query(
    UserModel.id,
    UserModel.username.label("name")
    UserModel.email
).outerjoin(
    UserContactModel, 
    and_( 
        UserModel.is == UserContactModel.user_id, 
        UserContactModel.country == 'india'
    )
).filter(UserModel.role == 'user').all()
Order by descending records SQLAlchemy
#Direct apply on model property
.order_by(UserModel.id.desc())

#by importing desc() method
from sqlalchemy import desc
session.query(UserModel).order_by(desc(UserModel.id)).all()
Column name as alias name SQLAlchemy
employees = self.session.query(
                EmployeeModel.id,
                EmployeeModel.name.label("emp_name") #we are using emp_name alias for column name
            ).filter(
                EmployeeModel.department == 'finance'
            ).all()
Update column values query in SQLAlchemy
session.query(
    UserModel
).filter(
    UserModel.id == 20
).update({
    UserModel.username: 'john_deo',
    UserModel.email: 'johndeo@domain.com',
    UserModel.city: 'New York'
})

session.commit()
Delete records query in SQLAlchemy
self.session.query(
    UserModel
).filter(
    UserModel.id == 100
).delete(synchronize_session=False)

self.session.commit()
Using OR on filter query SQLAlchemy
from sqlalchemy import or_

session.query(
    UserModel.id,
    UserModel.first_name
).filter(
    or_(
        UserModel.username == 'john', 
        UserModel.username == 'michael'
    )
).all()
Left Join Query python Sqlalchemy
session.query(
    Employee_Model.first_name,
    Employee_Model.last_name,
    Employee_Model.email,
    Employee_Address_Model.address
).join(
    Employee_Address_Model,
    Employee_Address_Model.employee_id == Employee_Model.id,
    isouter = True #Add this to implement left outer join
).all()
remove outliers
def outliers(column):
    Q1, Q3 = np.percentile(column, [25, 75])
    IQR = Q3 - Q1
    lower_range = Q1 - (1.5 * IQR)
    upper_range = Q3 + (1.5 * IQR)
    return lower_range, upper_range

outliers(df['yearly_salary'])
Split string to list python
my_str = "This is a string"
print(my_str.split())
#output - ["This", "is", "a", "string"]

comma_str = "Apple, Banana, Orange"
print(comma_str.split(", "))
#output - ["Apple", "Banana", "Orange"]
Column and data types in Sqlalchemy
Integer #integer type
SmallInteger #Small integer type
BigInteger #Big integer type
String #all string and character types
Text #variably sized string type
Boolean #Bool data type
Float #floating point data type
Enum #Generic Enum Type
Date #data type for datetime.date() objects
DateTime #data type for datetime.datetime() objects
Interval #data type for datetime.timedelta() objects
LargeBinary #large binary byte data
PickleType #Holds Python objects, which are serialized using pickle
Time #data type for datetime.time() objects
Like query Sqlalchemy
#record has 'test' keyword on any position
session.query(User).filter(User.first_name.like('%test%')).all()

#record has 'test' keyword on first position
session.query(User).filter(User.first_name.like('test%')).all()

#record has 'test' keyword on last position
session.query(User).filter(User.first_name.like('%test')).all()
Get all records Sqlalchemy
#get all records of first_name column
self.session.query(User.first_name).all()
Get first record Sqlalchemy
#Get vey first record from a table which has id grater than 10
session.query(User).filter(User.id > 10).first()
Join models or tables query Sqlalchemy
#JOINING TABLE (User and Address)
self.session.query(
    User.id,
    User.first_name,
    User.last_name,
    Address.city,
    Address.State
).join(
    Address, User.id == Address.user_id
).filter(
    Address.city.like('new%'))
).all()
Filter query Sqlalchemy
#SINGLE CONDITION
session.query(User).filter(User.id == 10).all()

#MULTIPLE CONDITION
session.query(User).filter(User.first_name == 'hello', User.last_name == 'world').first()

#LIKE QUERY
session.query(User).filter(User.name.like('e%')).all()
Data frame to excel using pandas
df.to_excel("~/Desktop/folder-name/filename.xlsx")
Reorder dataframe columns using column names in pandas
dframe = dframe[['column 5','column 6',4,3,2,1]]
Basic types and relationship in SQLAlchemy
from database import db, Base
from marshmallow import Schema, fields
from datetime import datetime as date

class Employee(db.Model):
    __tablename__ = "employee"
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, mssql_identity_start=100000)
    employee_uid = db.Column(db.Integer, unique=True)
    employee_email = db.Column(db.String, nullable=False)
    employee_name = db.Column(db.String(200), nullable=True)
    created_on = db.Column(db.DateTime, default=date.now())


class EmployeeAddress(db.Model):
    __tablename__ = "employee_address"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, mssql_identity_start=100000)
    employee_id = db.Column(db.Integer, db.ForeignKey('employee.id'), nullable=False)
    employee = db.relationship('Employee')
    employee_address = db.Column(db.Text, nullable=False)
Target database is not up to date - Alembic
flask db stamp head
flask db migrate
flask db upgrade
Sort list and dictionary in python
#SORT DICTIONARY ITEMS
my_subjects = {
    "math": { "sub_code": "M001", "score": 60 },
    "physics": { "sub_code": "P001", "score": 40 },
    "chemistry": { "sub_code": "C001", "score": 50 },
    "english": { "sub_code": "E001", "score": 45 }
}

#SORT DATA ASCENDING ORDER
sorted_data_asc = sorted( my_subjects.items(), key = lambda x: x[1]['score'] )
print(dict(sorted_data_asc))

#SORT DATA DESCENDING ORDER
sorted_data_desc = sorted( my_subjects.items(), key = lambda x: x[1]['score'], reverse=True )
print(dict(sorted_data_desc))

#SORT A LIST OF DICTIONARIES
my_list = [
    {"name": "John", "score": 30},
    {"name": "Deep", "score": 10},
    {"name": "Mark", "score": 50}
]

list_asc = sorted(my_list, key = lambda i: i['score'])
print (list_asc)

list_desc = sorted(my_list, key = lambda i: i['score'], reverse=True)
print (list_desc)
Remove a key from a dictionary Python
my_score = {
    "math": 90,
    "physics": 80,
    "chemistry": 85,
    "english": 91
}
#TO REMOVE 'physics' kEY from my_score
del my_score['physics']
Using ternary operator in Python
condition_value = 1
val = 1 if condition_value == 1 else 2
print val
Single line if else condition python
def hellPrint():
    myvar = "1"
    message = "Condition is true" if myvar == "1" else "Condition is false"
    print(message)
    
hellPrint()
Check PyTorch GPU Availability
In [1]: import torch

In [2]: torch.cuda.current_device()
Out[2]: 0

In [3]: torch.cuda.device(0)
Out[3]: <torch.cuda.device at 0x7efce0b03be0>

In [4]: torch.cuda.device_count()
Out[4]: 1

In [5]: torch.cuda.get_device_name(0)
Out[5]: 'GeForce GTX 950M'

In [6]: torch.cuda.is_available()
Out[6]: True
murata xlwing python任意の平均値、標準偏差、行数、列数のテーブル作成関数
@xw.func
@xw.ret(expand = "table")
def randomstd(mean, std, rows, columns):
    return np.random.normal(mean, std, (int(rows), int(columns)))
murata (xlwing使用)空白があるテキストファイル(数行)を読み込んで、excelに貼り付ける
xlwing 使用

テキストファイルの中身

test.txt
テスト test      t3    USD   44
te  55   99     gbe   99
pythonファイル

hello.py
def import_txt_split():

    # ブックの読み込み
    wb = xw.Book.caller()

    # シートの読み込み
    sht = wb.sheets['Sheet1']

    # テキストファイルの読み込み
    f = open('test.txt', 'r',encoding='UTF-8')

    # 各行をリストで取得
    datalist = f.readlines()

    # 空白を取り除く
    s = [i.split() for i in datalist]

    # 1行ずつ、リストをペッと貼り付ける
    for num,i in enumerate(s):
        sht.cells(num+1,1).value = i

    # テキストファイルを閉じる
    f.close()
VBA側

Sub import_text()
    RunPython ("import hello; hello.import_txt_split()")
End Sub
このVBAをボタンにマクロ登録。クリック

結果

qita1.png

上記の方法以外に、pandasのdataframeに貼り付けて、A1セルにぺっと貼り付ける方法でも出来た
Sqlalchemy basic db migration commands Python
python manage.py db init #INITIALIZE NEW MIGRATION
python manage.py db migrate #TO CREATE MIGRATION
python manage.py db upgrade #APPLY MIGRATION CHANGES ON DB
Practice
#import Pynput library
import Pynput
#import mouse from pynput
from pynput import mouse
#import controller from mouse
from pynput.mouse import Controller
mouse = Controller()
print(mouse.position)
Function in python
#SIMPLE FUNCTION
def function_name():
    print("Hello world")

function_name() #CALL FUNCTION


# FUNCTION WITH ARGUMENTS
def function_with_args(first_name, last_name):
    print("First name is " + first_name + ". Last name is" + last_name)

function_with_args("John", "Deo") #CALL FUNCTION WITH ARGUMENTS
#CALL WITH KEYWORDS ARGUMENTS
#ARGUMENT ORDER DOES NOT MATTER
function_with_args(last_name = "Deo", first_name = "John")


#WITH ARBITARY ARGUMENTS
#USE WHEN NUMBER OF ARGUMENT PASSED IS UNKNOWN
def function_with_arbitary_args(*args):
    print("Third item is " + args[2])

function_with_arbitary_args("Bat", "Bowl", "Hockey") #WILL PRINT 'HOCKEY'


#WITH ARBITARY KEYWORD ARGUMENTS
def function_with_arbitary_args(*args):
    print("Third item is " + args["last_name"])

function_with_arbitary_args(first_name = "Rick", last_name = "Bowl")
While loop python
counter = 1
while counter < 6:
    print(counter)
    counter += 1

#WITH ELSE STAEMENT
while counter < 6:
    print(counter)
    counter += 1
else:
    print("counter is greater or equals to 6");
List all files of a directory using os Python
from os import listdir
from os.path import isfile, join

dirPath = "path/to/my/directory"
allFileAndFolders = listdir(dirPath)

allFiles = [f for f in allFileAndFolders if isfile(join(dirPath, f))]
Get tofixed of a value using round() Python
val = 12.89
val1 = round(val, 0)
val2 = round(val, 1)

print(val1)
print(val2)
# 13.0
# 12.9
Float to int Python
float_val = 12.98;
print( int(float_val) ); 
# 12
Cut a portion of a video using moviepy
# pip install moviepy
# pip install imageio-ffmpeg

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

ffmpeg_extract_subclip("path/to/full/video.mp4", 30, 100, targetname="path/to/final/video.mp4")
# 30 - start second
# 100 - end second
Join two videos using moviepy in Python
from moviepy.editor import VideoFileClip, concatenate_videoclips

video_1 = VideoFileClip("path/to/first/video.mp4")
video_2 = VideoFileClip("path/to/second/video.mp4")

final_video= concatenate_videoclips([video_1, video_2], method="compose")
final_video.write_videofile("path/to/final/video.mp4")
Check S3 Bucket
import os
import boto3
from botocore.exceptions import ClientError

client = boto3.client(
    's3',
    # Hard coded strings as credentials, not recommended.
    aws_access_key_id='',
    aws_secret_access_key=''
)
s3 = boto3.resource(
    's3',
    # Hard coded strings as credentials, not recommended.
    aws_access_key_id='',
    aws_secret_access_key=''
)
        
# print(bucket_subfolder)
bucket = 'bucket-name'

for my_bucket_object in s3.Bucket(bucket).objects.all():
    print(my_bucket_object)
test url
test_url = 'http://httpbin.org/status/404'
convert pyqt5 resource file .qrs to python file?
pyrcc5 -o resources.py resources.qrc
Integer to String conversion in Python
intVal = 10
strVal = str(intVal)
PyQt5 Fixed Window Size
class MyDialog(QtWidgets.QDialog):

    def __init__(self):
        super(MyDialog, self).__init__()
        self.setFixedSize(640, 480)
Join two tuples in python
tuple1 = (1, 2, 3, 4)
tuple2 = (5, 6, 7, 8)

tuple3 = tuple1 + tuple2
print(tuple3)
Multiple constructors in python
class Cheese(object):
    def __init__(self, num_holes=0):
        "defaults to a solid cheese"
        self.number_of_holes = num_holes

    @classmethod
    def random(cls):
        return cls(randint(0, 100))

    @classmethod
    def slightly_holey(cls):
        return cls(randint(0, 33))

    @classmethod
    def very_holey(cls):
        return cls(randint(66, 100))


# Now create object like this:

gouda = Cheese()
emmentaler = Cheese.random()
leerdammer = Cheese.slightly_holey()


# another example

class C(object):

    def __init__(self, fd):
        # Assume fd is a file-like object.
        self.fd = fd

    @classmethod
    def fromfilename(cls, name):
        return cls(open(name, 'rb'))

# Now you can do:
c = C(fd)
# or:
c = C.fromfilename('a filename')
Jupyter Notebooks
#store variables
%store stream_whole
#read variables
%store -r stream_whole
Evaluate,check before class feald values set, bothe constructor,property option
class Product(object):

    def __init__(self, price = 0.0, name = ""):
        self.price = price
        self.name = name

    # property for __price attribute
    @property
    def price(self):
        return self.__price

    @price.setter
    def price(self, value):
        if value < 0:
            raise ValueError("Price cannot be negative")
        self.__price = value

    # property for __name attribute
    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, value):
        for ch in value:
            if ch.isdigit():
                raise Exception("Enter valid product name")
        self.__name = value
Pandas DataFrame to CSV
# Use custom separator	sep	
df.to_csv('file_name.csv',sep='\t')
# Write without index	index	
df.to_csv('file_name.csv',index=False)
# Write without headers	header	
df.to_csv('file_name.csv',header=False)
# Write subset of columns	columns	
df.to_csv('file_name.csv',columns=['col_A','col_B'])
# Change file encoding format	encoding	
df.to_csv('file_name.csv',encoding='utf-8')
# Change NaNs as Unknown	na_rep	
df.to_csv('file_name.csv',na_rep='Unknown')
Hello World
print("Hello World")
test_3
print("This the second example")
test_2
print("haelo")
murata private 複数エクセルファイル内の値をまとめて1つのCSVファイルに出力する
import pathlib  # 標準ライブラリ
import openpyxl # 外部ライブラリ pip install openpyxl
import csv      # 標準ライブラリ

# ワークブックを作成
new_workbook = openpyxl.Workbook()          
# デフォルトで作成されるワークシートを選択
new_sheet = new_workbook.active                   


new_row = 1

# ..\ → このプログラムの親フォルダを示す。(.\だけだとカレントフォルダを示す。)
path = pathlib.Path("..\data\targetfolder")    #相対パス指定

#  path.iterdir() → ディレクトリ内のファイル・サブディレクトリ一覧を取得
for all_obj in path.iterdir():
    # 拡張子がxlsxかを判定
    if all_obj.match("*.xlsx"):
        # ワークブックの読み込み  //最初からglobを使用すればいいのでは?
        wb = openpyxl.load_workbook(all_obj)
        # ワークブックからシートを順に読み込み
        for target_sheet in wb:
            #シートの9行目から18行目までを操作するように指示    //xlupのように始まりと終わりが分からない場合に対応させるには?
            for target_row in range(9,19):
                # 対象行の2番目の列に、データが入力されいるかを調べる。
                if target_sheet.cell(target_row, 2).value != None:
                    #より説明的なコード
                    #new_sheet.cell(row=new_row, column=1).value = \
                    #    target_sheet.cell(row=2, column=7).value   #伝票NO

                    #対象ファイルの2行目7列目を、作成したシートの対象行のlist_row行目の1列目に転記
                    new_sheet.cell(new_row, 1).value = target_sheet.cell(2, 7).value   #伝票NO
                    # 以下同様の考え
                    new_sheet.cell(new_row, 2).value = target_sheet.cell(3, 7).value   #日付
                    new_sheet.cell(new_row, 3).value = target_sheet.cell(4, 3).value   #得意先コード
                    new_sheet.cell(new_row, 4).value = target_sheet.cell(7, 8).value   #担当者コード

                    #対象ファイルのdt_row行目の1列目を、作成したシートのlist_row行目の1列目に転記
                    new_sheet.cell(new_row, 5).value = target_sheet.cell(target_row, 1).value #No  
                    #以下同様                  
                    new_sheet.cell(new_row, 6).value = target_sheet.cell(target_row, 2).value #商品コード 
                    new_sheet.cell(new_row, 7).value = target_sheet.cell(target_row, 3).value #商品名
                    new_sheet.cell(new_row, 8).value = target_sheet.cell(target_row, 4).value #数量
                    new_sheet.cell(new_row, 9).value = target_sheet.cell(target_row, 5).value #単価
                    
                    # 「\」 → 行継続の記号
                    #  対象ファイルのdt_row行目の4列目と5列目の積を、作成したシートのlist_row行目の10列目に転記
                    new_sheet.cell(new_row, 10).value = target_sheet.cell(target_row, 4).value * \
                                                target_sheet.cell(target_row, 5).value #金額
                    new_sheet.cell(new_row, 11).value = target_sheet.cell(target_row, 7).value #備考   

                    # 新しいデータ下に追加できるように、list_rowに1を加算                                
                    new_row += 1

#new_workbook.save("..\data\sales\salesList.xlsx")

# ファイルの保存

# 引数として、ファイル名、モード、エンコーディングを指定
with open("..\newList.csv","w",encoding="utf_8_sig") as fp:
    
    writer = csv.writer(fp, lineterminator="\n")

    # 作成したシートの各行を取得
    for row in new_sheet.rows:
        # 各行から各列を取り出し、リストにする。
        # その1行分のリストをwriter.writerow()により、csvとして出力する。
        writer.writerow([col.value for col in row])
html hsdata_file_name_change(途中)
import re
import glob
import os
import shutil
import csv


# 同フォルダ内のhtmlファイル名を取得
file_name = glob.glob('*.csv')[0]

if "~" not in file_name:
    print("実行する")
    
    # ここにコードを記述
    
    f = open(file_name)
    reader = csv.reader(f)
    
    l = [row for row in reader]
    
    beginning=l[1][0]
    
    ending=l[-1][0]
    
    beginning.split("/")
    start_date=beginning.split("/")[2] + "-" + beginning.split("/")[0] + "-" + beginning.split("/")[1]
    
    ending.split("/")
    end_date=ending.split("/")[2] + "-" + ending.split("/")[0] + "-" + ending.split("/")[1]
    
    start_date + "~" + end_date
    
    new_file_name=file_name.replace("FromDownloader","")
    new_file_name=new_file_name.replace("_BidAndAsk","")

    new_file_name =  new_file_name + "["+start_date + "~" + end_date+"]" 
    
    f.close()
    
    os.rename(file_name,new_file_name)
else:
    print("実行しない")
murata 正規表現 M30 (2003.11.01 - 2019.12.01)の抽出(桁数と改良余地あり)
text = "barabrara M30 (2003.11.01 - 2019.12.01)  brarbrarbab"

re.findall(r'[A-Z][0-9]+ \([0-9]+\.[0-9]+\.[0-9]+ - [0-9]+\.[0-9]+\.[0-9]+\)',text)[0]
murata excel 1行目にヘッダー見出しをつける関数
# osh シートオブジェクト名と仮定

def print_header():
    osh["A1"].value = "担当者"
    osh["B1"].value = "数量"
    osh["C1"].value = "金額"
    osh["D1"].value = "得意先"
    osh["E1"].value = "数量"
    osh["F1"].value = "金額"
murata not public netkeiba data processing function
# 前提
# スクレイピングしたデータをresults.pickleというファイル名で取得済


import pandas as pd

# データの読み込み
results=pd.read_pickle('results.pickle')

def preproccessing(results):
    # 元のデータを変更しないために、コピー
    df = results.copy()
    # 着順に数字以外の文字列が含まれているものを取り除く
    df = df[~(df['着順'].astype(str).str.contains("\D"))]
    # 整数型に変換
    df['着順'] = df['着順'].astype(int)
    
    # 性齢を「性」「年齢」に分ける
    df['性'] = df['性齢'].map(lambda x:str(x)[0])
    df['年齢'] = df['性齢'].map(lambda x:str(x)[1:]).astype(int)
    
    # 馬体重を「体重」と「体重変化」に分ける
    df["体重"] = df["馬体重"].str.split('(', expand= True)[1].str[:-1].astype(int)
    df["体重変化"] = df["馬体重"].str.split('(', expand= True)[1].str[:-1].astype(int)
    
    # 単勝の型をfloat型に変換
    df['単勝'] = df["単勝"].astype(float)
    
    # 不要な列を削除
    df.drop(['タイム','着差','調教師','性齢','馬体重'],axis=1,inplace=True)
    
    return df
Python collection - Dictionaries in python
my_dict = {
    "first_name": "John",
    "last_name": "Deo",
    "occupation": "programmer"
}

print(my_dict["first_name"]) #print first_name value of dictionary

print(my_dict.get("first_name")) #same as above

my_dict["first_name"] = "Josef" #To change first_name value

my_dict.values() #return values of dictionary

my_dict["nick_name"] = "JDo" #To add key value to dictionary

my_dict.pop("last_name") #To remove a key value pair from dictionary based on key

del my_dict["last_name"] #Same as above

my_dict.clear() #empty a dictionary

my_dict.copy() #To copy a dictionary
Python collection - sets in python
my_set = {"item_1", "item_2", "item_3"}

"""
sets are unordered and unindexed 
so it can not be accessed by using index
"""

for (item in my_set):
    print(item)

my_set.add("item_4") #To add single item to set

my_set.update(["item_4", "item_5"]) #To add multiple items to set

my_set.remove("item_3") #To remove an item

del my_set #To remove set
Python collection - Tuples in python
#Tuples are unchangable means you can not modify its items
fruits = ("banana", "orange", "apple", "grapes")

print(fruits[2]) #print third element apple

print(fruits[:3]) #print items start index to 0 and less than index 3

print(fruits[1:3]) #print tuple start from index 1 and less than index 3

print(fruits[-1]) #print last element
murata not public netkeiba scraping
import pandas as pd
import time
# プログレスバーの表示
from tqdm import tqdm_notebook as tqdm

# 対象のurlの構造分析
# 'https://db.netkeiba.com/race/202006030301'

# ex.
# 202006030301について
# 2020 → 西暦
# 06 → 開催場 ('札幌', '函館', '福島', '新潟', '東京', '中山', '中京', '京都', '阪神', '小倉')
# 03 → 上記開催場の何回目
# 03 → 上記開催場の上記回の何日目
# 01 → レース番号

race_id_list = []
# 開催上
for place in range(1,11,1):
    # ~回
    for kai in range(1,6,1):
        # ~日
        for day in range(1,9,1):
            # ~R
            for r in range(1,13,1):
                race_id = "2019"+ str(place).zfill(2)+str(kai).zfill(2)+str(day).zfill(2) + str(r).zfill(2)
                race_id_list.append(race_id)   


# 途中中断し、再開した時を想定し、読み込み済みの結果を第2引数に
def scrape_race_results(race_id_list,pre_race_results={}):
    # 既に読み込み済の結果を反映
    race_results = pre_race_results
    # tqdmにより、進捗を表示させる
    for race_id in tqdm(race_id_list):
        
        # レースIDが読み込み済のリスト内にある場合は、ここで処理を中断し、for文へ戻す
        if race_id in race_results.keys():
            print(race_id)
            continue
        try:
            url = "https://db.netkeiba.com/race/" + race_id
            race_results[race_id] = pd.read_html(url)[0]
            time.sleep(1)
            
        # 該当のIDにレース情報がなかった場合にも中断しないようにする。
        except IndexError:
            continue
        # スクレイピング自体を中断しても、race_resultsを返してくれるようにする。
        except:
            break
    return race_results


 # テスト中断して実行するときに、左辺と右辺をそれぞれ1ずつずらして実行する(ex. test3 = ~ test2)
 test2 = scrape_race_results(race_id_list,test)

# 取得した各レースの結果データ(今は辞書型でそれぞれ分かれている)をつなげる処理
# つなげた際に識別できるように、IDをレースIDにしておく。

# 全てのレースIDを一つずつ取り出し、
for key in test.keys():
    # 各着版ごとにつけられているインデックスをレースIDに変換。長さは各レースのデータフレームの長さ(馬の数)分だけ置き換える。
    test[key].index = [key]*len(test[key])

# 全てをつなげ、1つのデータフレームにまとめる。行が入れ替わらないようにsort=Falseを設定。
results = pd.concat([test[key] for key in test.keys()], sort=False)

# pickle形式で保存
results.to_pickle('results.pickle')

# csvで保存
results.to_csv("results.csv", encoding='utf_8_sig')
Python collection - List in python
list = ["pen", "pencil", "sharpner", "eraser", "board"]

print(list[2]) #print third element sharpner

print(list[:3]) #print items start index to 0 and less than index 3

print(list[1:3]) #print list start from index 1 and less than index 3

print(list[-1]) #print last element
Arbitrary Arguments *args in python
def favourite_fruit(*args):
    print("My favourite fruit is : " + args[1])

favourite_fruit("Apple", "Mango", "Banana")
Create a function in python
def function_name():
    print("Hello this is a function")

#To call the above function use
function_name()
For loop python
#LOOP THROUGH LIST
names = ["John", "Sonia", "Rakesh"]
for item in names:
    print(item)

#LOOP THROUGH DICTIONARY
my_dict = {
    "name": "John",
    "title": "Programmer",
    "lang": "Python"
}

for key in my_dict:
    print(key, "is: ", my_dict[key])

# Loop through list of dictionaries
subjects = [
    { "name": "English", "score": 90 },
    { "name": "Math", "score": 100 },
    { "name": "Physics", "score": 90 }
]

for subject in subjects:
    print("Subject Name is: ", subject['name'], " Score is: ", subject['score'])
If else conditions python
x = 50
y = 30
if x > y:
    print("x is greater than y")
elif x == y:
    print("x equals to y")
else:
    print("y is greater than x")
Add comments in python
#Write comment text here
print("Hello World 1!")

print("Hello World 2!") #OR like this

"""
This is a multiline comment
You can add as much lines as you want
"""
print("Hello World 3!")
murata excel転記
# masters_copy.py
import openpyxl


# 1) エクセルファイルを開く
wb = openpyxl.load_workbook("sample.xlsx")
ws = wb["住所"]

# 重要住所の行
masters_rows = []

header_row = ws[1]

# 2) エクセルファイルを1行ずつ読み込み(2行目から)、
for row in ws.iter_rows(min_row=2):
    # マークの付いている行だけをリストに入れておく
    if row[2].value == "〇":
        masters_rows.append(row)

        
masters_rows.insert(0,header_row)


# 3) 新規シートを作成する
ws2 = wb.create_sheet(title="重要住所")



# 4) 新規シートにリストに入れておいた行を転記する
for master_row in masters_rows:
    ws2.append([r.value for r in master_row])

# 3列目削除

ws2.delete_cols(3)

wb.save("sample.xlsx")
    
wb.remove(ws)
# 5) エクセルファイルを保存する
wb.save("重要住所.xlsx")
murata 途中 要改善 pyautoguiを使ったExcelからテキストファイルへの転記
import pyautogui as pg
import time
import pyperclip


x,y=pg.locateCenterOnScreen('./picture_for_gui/yuubinn.png',confidence=0.1) 
for i in range(10):
    
    if i == 0:
        pg.click(1223,1064,duration=0.5)
        pg.moveTo(x,y,duration=1)
        
        pg.press('down') 

        pg.hotkey('ctrl','c')
        
        time.sleep(1)

        pg.click(1305,1045,duration=0.5)

        pg.click()

        time.sleep(1)

        pg.hotkey('return')

        pg.typewrite('yuubinnbangou;\n')

        pg.hotkey('ctrl','v')

        pg.click(1223,1064,duration=0.5)

        pg.press('tab')
        pg.hotkey('ctrl','c')

        pg.click(1305,1045,duration=0.5)

        pg.hotkey('return')

        pg.typewrite('jyuusyo;\n')
        pg.hotkey('ctrl','v')
        
        continue
        


    pg.click(1223,1064,duration=0.5)

    pg.click()
    
    pg.hotkey('ctrl','left')
    
    pg.press('down') 

    pg.hotkey('ctrl','c')

    time.sleep(1)

    pg.click(1305,1045,duration=0.5)

    pg.click()

    time.sleep(1)

    pg.hotkey('return')

    pg.typewrite('yuubinnbangou;\n')

    pg.hotkey('ctrl','v')

    pg.click(1223,1064,duration=0.5)

    pg.press('tab')
    pg.hotkey('ctrl','c')

    pg.click(1305,1045,duration=0.5)

    pg.hotkey('return')

    pg.typewrite('jyuusyo;\n')
    pg.hotkey('ctrl','v')
murata ファイルを拡張子ごとにフォルダへ振り分け
import os
import pathlib
import shutil

# 「Untitled.html」というようなファイル名+拡張子のリスト用
file_list = []


# after_file_list = []

# . が含まれる者すべてを抽出
for file in os.listdir("."):
    # ファイルかどうかを判定し、フォルダ等を除外
    is_file = os.path.isfile(file)
    
    # このpyファイル自身でないか
    # jupyter notebookの場合、エラーになるので、直接ファイル名入力
    not_this_file = os.path.basename(__file__) != file
    
    if is_file and not_this_file:

        file_list.append(file)
        # after_file_list.append(after_file_name)

current_dir = os.getcwd()

for target_file in file_list:

    # 拡張子を抽出
    target_ext = target_file.split(".")[-1]
    # 拡張子が付いた新しいフォルダパスを作成
    new_folder_path = os.path.join(current_dir,target_ext)
    
    # 現在のファイルパスを作成
    current_file_path = os.path.join(current_dir,target_file)
    
    # 新しいファイルパスを作成
    new_file_path = os.path.join(new_folder_path,target_file)
    
    # 新しく拡張子名のフォルダを作成。
    os.makedirs(new_folder_path,exist_ok=True)

    # ファイルを移動。(既存のファイルがあってもエラーにならないように、絶対パスで記述)
    shutil.move(current_file_path, new_file_path)
murata フォルダ内のファイルをcsvにまとめる
import os
import csv
import time

csv_file = 'filelist.csv'
date_format = '%Y/%m/%d %H:%M:%S'

file_list = []

for file in os.listdir("."):
    # ファイルかどうか
    is_file = os.path.isfile(file)
    # このpyファイル自身でないか
    not_py_file = os.path.basename(__file__) != file
    # リストCSVファイルでないか
    not_csv_file = csv_file != file

    if is_file and not_py_file and not_csv_file:
        # ファイル作成時刻
        time_crt = time.strftime(date_format,
                                 time.localtime(os.path.getctime(file)))
        # ファイル更新時刻
        time_mod = time.strftime(date_format,
                                 time.localtime(os.path.getmtime(file)))

        file_list.append([file, time_crt, time_mod])

with open(csv_file, "w", newline="") as f:
    csv_writer = csv.writer(f)
    for r in file_list:
        csv_writer.writerow(r)
murata htmlの文字変換(K,M →数字)
def main():


    import re
    import glob
    import os
    import shutil

    # 同フォルダ内のhtmlファイル名を取得
    file_name = glob.glob('*.html')[0]

    # 対象ファイルの絶対パスを取得
    path = os.path.join(os.getcwd(),file_name)

    # beforeフォルダの絶対パスを取得
    before = os.path.join(os.getcwd(),"before")
    # afterフォルダの絶対パスを取得
    after = os.path.join(os.getcwd(),"after")

    # フォルダの新規作成(既にある場合は作らない)
    if not os.path.exists(before):
        os.makedirs(before)
    if not os.path.exists(after):
        os.makedirs(after)

    s = open('./'+file_name, 'r+',encoding="utf-16")

    data_lines = s.read()

    x=re.findall(r'[1-9]{1,4}.?[0-9]{0,8}[K,M]',data_lines)

    def km_change(text):
        if "K" in text:
            changed = format(float(text.replace("K","0"))*1000, '.2f')
        elif "M" in text:
            changed = format(float(text.replace("M","0"))*1000000, '.2f')
            
        return changed

    for i in x:
        data_lines=data_lines.replace(i,km_change(i))

    # afterフォルダへの絶対パスを取得
    changed_file=os.path.join(after,file_name)

    # beforeフォルダへの絶対パスを取得
    before_file=os.path.join(before,file_name)


    with open(changed_file, mode="w", encoding="utf-16") as f:
        f.write(data_lines)

    s.close()

    # 元ファイルをbeforeフォルダへ移動
    shutil.move(path, before_file)
    

if __name__=='__main__':
    main()
Demo
class Demo:

    def __init__(self):
        
        self.a = 10
        self.b = 20

    def get_total_of_a_b:
        
        total = self.a + self.b
        return total
[Python] Using inspect.stack() to get module name, path, function name
import pytest
import logging
import inspect

def test_inspect():
    caller = inspect.stack()[1]
    logging.info(f"\nThis is  caller[3]:  {caller[3]}")

    def foo():
        caller = inspect.stack()[1]
        logging.info(f"\nThis is  caller[3] inside function:  {caller[3]}")

        for item in caller:
            logging.info(f"\nThis is  caller item:  {item}")
    foo()
[Python] Simple yield generator example
# Generator function
def generator(count):
        i = 0
        while i < count:
            yield i
            i += 1

def test_generator():
    for value in generator(4):
        print("\nGenerated value :", value)
    pass
[Python] Default value of function argument
# Example demonstrates usage of  function arguments default values

def foo(filename='testid', flag = True, number = None):
    pdf_file = filename + ".pdf"
    print(f'\n Filename =  {pdf_file}, Flag = {flag}, number = {number}')


if __name__ == '__main__':
    foo()
    foo("readme", False, 4)
[Python] Get nodeid, module, function name using Pytest request built-in fixture
# to enable logging add  to pytest.ini the following:
# [pytest]
#log_cli = 1
# log_level = INFO
# log_cli_level = INFO
# log_cli_format = %(asctime)s %(levelname)s %(message)s
# log_cli_date_format = %H:%M:%S


import pytest
import logging

# Example 1
def test_node_id(request):
    node = request.node.nodeid
    logging.info(f"\nThis is node:  {node}")

    module_name = request.module.__name__
    logging.info(f"\nThis is module name:  {module_name}")

    function_name = request.function.__name__
    logging.info(f"\nThis is function name:  {function_name}")

    fspath = request.fspath
    logging.info(f"\nThis is fspath :  {fspath}")

# Example 2
def test_func_node_id(request):
    req = request
    def foo(r):
        node = r.node.nodeid
        logging.info(f"
This is function node:  {node}")
    foo(req)
[Python] Two ways to import a module
# Option 1: we have to specify 'sys'
import sys

if len(sys.argv) == 1:
    print("No arguments specified")
    print("This is the name of the script:", sys.argv[0] )


# Option 2
from sys import argv
if len(argv) == 1:
    print("No arguments specified")
    print("This is the name of the script:", argv[0] )
[Python] Save (write) data to file
myfile = open('hello.txt', 'w')
myfile.write('Hello world')
myfile.close()
[Python] Eval() function
# The eval() method parses the expression passed to this method 
# and runs python expression (code) within the program.
x = 1
print(eval('x + 1'))
[Python] Function parameters - pack and unpack
# Asterisk turns parameter into tuple reference
def average(*nums):
    result = sum(nums)/len(nums)
    return result

# Example 1
x = average(10,20,30)
print('Example 1(pack): ',x)

# Example 2
x = average(10,20,30,40,50)
print('Example 2(pack): ', x)

# Asterisk unpacks tuple before sending to the function
# Example 3
mytuple = (10,20,30)
x = average(*mytuple)
print('Example 3(unpack): ', x)
[Python] Function returns multiple values
def my_func():
    return 1,2

x,y = my_func()
print(x)
print(y)
[Python] Sort a list
mylist = [900, 20, 300, 5, 90]

mylist.sort()
for x in mylist:
    print(x)
[Python] Slice of tuple
mytuple = (10,20,30,45,60,70)

for x in mytuple[0:3]:
    print(x)
    print(============)

# iterate through first 2 numbers
for x in mytuple[:2]:
print(x)
print(============)

# iterate through last (6-4)=2 numbers
for x in mytuple[4:]:
    print(x)
    print(============)

mystring= "abcdefghijkl"

# iterate through string symbols starting fron 2nd, skip every 2 symbols
for x in mystring[2::2]:
    print(x)
[Python] Range - standard function to create a list of numbers
for a in range(1,11):
    print("a is ", a)
[Python] Create a list of objects using class names
# In Python, the name of the class refers to the class instance. Consider:

class A: pass
class B: pass
class C: pass

lst = [A, B, C]

# instantiate second class
b_instance = lst[1]()
print(b_instance)
[Python] F-string format datetime
import datetime

now = datetime.datetime.now()

print(f'{now:%Y-%m-%d %H:%M}')

# The example displays a formatted current datetime. The datetime format specifiers follow the : character.

# This is the output.
2019-05-11 22:39
[Python] Inheritance and adding variable. Page object example
# page object of Dashboard page. SitePAge - parent class
class SiteDashboardPage(SitePage):
    PAGE_TITLE = (By.CLASS_NAME, 'page-title')

    def __init__(self, browser):
        SitePage.__init__(self, browser)
        self.page_title = self.browser.find_element(*self.PAGE_TITLE).text
[Python] Using comprehension expression to convert list of dictionaries to nested dictionary
# yaml file used for account 
- sitename: testcomp
  email: test@outlook.com
  pass: 111
  role: admin
  name: admin
- sitename: testcomp
  email: mvsubmit@gmail.com
  pass: 111
  role: edit
  name: user1
- sitename: testcomp
  email: test2@outlook.com
  pass: 11
  role: edit
  name: user2

# convert.py
    # read yaml file into faccounts
    faccounts = yaml.load(file, Loader=yaml.FullLoader)
    # Convert list of dictionaries to nested dictionary so that we can select any value by user name and key nam
    account = {account['name']: account for account in faccounts}
    return account
[Python] Pytest fixture example with cleanup and teardown
# Deafult scope=function
@pytest.fixture
def browser():
    # Initialize Chrome driver
    driver = Chrome()

    # Wait implicitly for elements to be ready before attempting interactions
    driver.implicitly_wait(10)

    # Return the driver object at the end of setup
    yield driver

    # For cleanup, quit the driver
    driver.quit()
[Python] F-strings format syntax call functions
>>> name = "Eric Idle"
>>> f"{to_lowercase(name)} is funny."
'eric idle is funny.'

>>> f"{name.lower()} is funny."
'eric idle is funny.'
[Python] F-strings format syntax
>>> F"Hello, {name}. You are {age}."
'Hello, Eric. You are 74.'
[Python] Different ways to test multiple flags at once in Python
# Different ways to test multiple
# flags at once in Python
x, y, z = 0, 1, 0

if x == 1 or y == 1 or z == 1:
    print('passed')

if 1 in (x, y, z):
    print('passed')

# These only test for truthiness:
if x or y or z:
    print('passed')

if any((x, y, z)):
    print('passed')
[Python] The get() method on Python dicts and its "default" arg
# The get() method on dicts
# and its "default" argument

name_for_userid = {
    382: "Alice",
    590: "Bob",
    951: "Dilbert",
}

def greeting(userid):
    return "Hi %s!" % name_for_userid.get(userid, "there")

>>> greeting(382)
"Hi Alice!"

>>> greeting(333333)
"Hi there!"

''' When "get()" is called it checks if the given key exists in the dict.

If it does exist, the value for that key is returned.

If it does not exist then the value of the default argument is returned instead.'''
[Python] Namedtuples can be a great alternative to defining a class manually
# Using namedtuple is way shorter than
# defining a class manually:
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')

# Our new "Car" class works as expected:
>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
>>> my_car.mileage
3812.4

# We get a nice string repr for free:
>>> my_car
Car(color='red' , mileage=3812.4)

# Like tuples, namedtuples are immutable:
>>> my_car.color = 'blue'
AttributeError: "can't set attribute"
[Python] Use json.dumps() to pretty-print Python dicts
# The standard string repr for dicts is hard to read:
>>> my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee}
>>> my_mapping
{'b': 42, 'c': 12648430. 'a': 23}  # 😞

# The "json" module can do a much better job:
>>> import json
>>> print(json.dumps(my_mapping, indent=4, sort_keys=True))
{
    "a": 23,
    "b": 42,
    "c": 12648430
}

# Note this only works with dicts containing
# primitive types (check out the "pprint" module):
>>> json.dumps({all: 'yup'})
TypeError: keys must be a string
Function argument unpacking in Python
def myfunc(x, y, z):
    print(x, y, z)

tuple_vec = (1, 0, 1)
dict_vec = {'x': 1, 'y': 0, 'z': 1}

>>> myfunc(*tuple_vec)
1, 0, 1

>>> myfunc(**dict_vec)
1, 0, 1
Python Measure the execution time of small bits of Python code with the timeit module
>>> import timeit
>>> timeit.timeit('"-".join(str(n) for n in range(100))',
                  number=10000)

0.3412662749997253

>>> timeit.timeit('"-".join([str(n) for n in range(100)])',
                  number=10000)

0.2996307989997149

>>> timeit.timeit('"-".join(map(str, range(100)))',
                  number=10000)

0.24581470699922647
Pandas - Delete,Remove,Drop, column from pandas DataFrame
# Opiton one
del df['column_name']

# The best way to do this in pandas is to use drop:
df = df.drop('column_name', 1)

# where 1 is the axis number (0 for rows and 1 for columns.) To delete the column without having to reassign df you can do:
df.drop('column_name', axis=1, inplace=True)

# Finally, to drop by column number instead of by column label, try this to delete, e.g. the 1st, 2nd and 4th columns:
df = df.drop(df.columns[[0, 1, 3]], axis=1)  # df.columns is zero-based pd.Index this is called drop by index

# Another way is:
columns = ['Col1', 'Col2', ...]
df.drop(columns, inplace=True, axis=1)
Pandas - Append multiple pandas data frames at once
new_df = pd.concat([df1, df2, df3, df4, df5])
#ore
new_df = pd.concat([df1, df2, df3, df4, df5], ignore_index=True)
Pandas - How to check whether a pandas DataFrame is empty
if df.empty:
    print('DataFrame is empty!')
Python - String, renove all single character for text
# renove all single character for text

text = "t is a  c         test for removing                         multiple spaces"

document =' '.join( [w for w in text.split() if len(w)>1] )

print(document)
Python - String ,remove,delete,replace,extract numbers from string
# remove numbers from string
s = '12abcd405'
result = ''.join([i for i in s if not i.isdigit()])
Python - regex , remove all single characters,replace all single chars,char
# remove all single characters
# replace all single chars,char
import re

text = "That is a           test for removing                         multiple spaces"

document = re.sub(r's+[a-zA-Z]s+', ' ', text)

print(document)
Python - regex,replace all character exept A-Z a-z and numbers
# Remove all the special characters
# replace all character exept A-Z a-z
import re

text = "That is a # ; '' 2 45 6 ?/..,,, test for removing                         multiple spaces"

document = re.sub(r'W', ' ', text)

print(document) # as we can see this regex expresion do not remove numbers
Python - regex , replace multiple spaces with one space
# Substituting multiple spaces with single space
# replace multiple spaces with one space
# delete spaces
# spaces delete 
# tabs replace with one space 
# tow ore more spaces betwen words 
import re

text = "That is a           test for removing                         multiple spaces"
document = re.sub(r's+', ' ', text, flags=re.I)

print(document)
Python - String , Check If Word Is In string
if word in mystring: 
   print ('success')


# or


if 'seek' in 'those who seek shall find':
    print('Success!')
# Python Program - Convert Hexadecimal to Binary
print("Enter 'x' for exit."):
hexdec =input("Enter any number in Hexadecimal Format: "):
if hexadec == 'x':
    exit():
    else:
        dec=int(hexdec,16):
        print(hexdec,"in Binary ="bin(dec)):
# Python Program - Pattern Program 2
k=1
for i in range(0,5):
    for j in range(0,k):
        print("* ",end=" ")
        k=k+2
        print()
Pattern
for i in range (0,5):
    for j in range (0,i+1):
        print("* ",end="")
        print()
Text Editor with Python
from tkinter import Tk,Menu,filedialog,END,messagebox
import tkinter.scrolledtext as ScrolledText


#Global File Name
filename = 'Untitled.txt'

#root for main window 
root = Tk(className = " Text Editor Noob")
root.iconbitmap(r'icon.ico')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
textArea = ScrolledText.ScrolledText(root,width=100, height =80)

#FUNCTIONS
#Open file function
def openFile():
    file =filedialog.askopenfile(parent=root,title="Please Select a file to open",filetypes=(("Text File","*.txt"),("All Files","*")))

    if file != None:
        contents = file.read()
        textArea.insert('1.0',contents)
        file.close()

#Save as File function
def fileSaveAs():
    file = filedialog.asksaveasfile(mode="w",defaultextension=".txt")
    if file != None:
        #slice of the last character from get,as an extra return (enter) is added
        data = textArea.get('1.0',END+'-1c')
        file.write(data)
        file.close()

#Exit Function 
def quitroot():
    if messagebox.askyesno("Quit Text Editor Noob","Are you sure you want to quit?Because Biswas will mind if you quit X0X0"):
        root.destroy()

#About function
def About():
    label = messagebox.showinfo("About TextEditor Noob","An alternative notepad made with the help of Python and Developed By Biswas Sampad Satpathy")


#Menu options
menu = Menu(root)
root.config(menu=menu)
#top Menu
fileMenu = Menu(menu)
menu.add_cascade(label=" File",menu = fileMenu)
#sub menus
fileMenu.add_command(label="New")
fileMenu.add_command(label="Open",command = openFile)
fileMenu.add_command(label="Save As",command = fileSaveAs)
fileMenu.add_command(label="Print")

fileMenu.add_separator()
fileMenu.add_command(label="Close",command = quitroot)
#top menu
helpMenu = Menu(menu)
menu.add_cascade(label="Help",menu=helpMenu)
helpMenu.add_command(label="Help")
#top menu
aboutMenu = Menu(menu)
menu.add_cascade(label="About",command=About)


textArea.pack()


#keep window open
root.mainloop()
First highest views based recommendation system in python part 1
df1 = pd.DataFrame(data1)
        df2 = pd.DataFrame(data2)
        view_data = df1.drop(["timestamp","user_email","user_name"],axis=1)
        movie_df = pd.merge(df2, view_data, on="movie_id")
        movie_grouped = movie_df.groupby(['movie_name','movie_id']).agg({'no_of_views': 'count'}).reset_index()
        grouped_sum = movie_grouped['no_of_views'].sum()
        movie_grouped['percentage']  = movie_grouped['no_of_views'].div(grouped_sum)*100
        movie_final = pd.DataFrame(movie_grouped)
        count = pd.DataFrame(movie_final)
        final_out = pd.merge(count, view_data, on="movie_id")
        final_up =final_out.drop_duplicates(subset='movie_id', keep='first', inplace=False)
        final = final_up.drop(["no_of_views_x","no_of_views_y","percentage"],axis=1)
        # converting it to csv with store id
        final_csv = final.to_csv('public/csv/'+str(studio_id)+'.csv')
        # opening the exported csv file to save to mongodb 
        f = open('public/csv/'+ str(studio_id) +'.csv','rU')
        #db URI
        col = connect_db(server).recomendations
        #reading csv
        reader = csv.DictReader(f)
        #askign desired header
        fieldnames=("movie_name","movie_id","user_id","customer_id","store_id")
        # loop to dismantle the dictionary as per the
        if(mode == 1):
            coly = connect_db(server).recomendations.delete_many({"customer_id":str(studio_id)})
        else:
            coly = connect_db(server).recomendations.delete_many({"store_id":str(studio_id)})
        # colx = connect_db(server).recomendations.delete_many({"store_id_x":"101"})
        for each in reader: 
            row={}
            for field in fieldnames:    
                row[field] = each[field]    
            col.insert(row)
        # cleaning the dataframes
        df1.iloc[0:0]
        df2.iloc[0:0]
        #deleting the csv after exporting to database
        # os.remove('public/csv/'+str(studio_id)+'.csv')
        print('Training Done !!')
To get url param details from get request in Django
server = request.GET.get('server_info','production')
    count = request.GET.get('limit_count')
    user_id = request.GET.get('user_id')
    customer_id = request.GET.get('alie_customer_id',None)
    studio_id = request.GET.get('studio_id',None)
    print(count)
    print(user_id)
    print(customer_id)
    print(studio_id)
Clone conda environment
conda create --name myclone --clone base
Create python virtual environment command
virtualenv venv -p python3