Saturday, April 4, 2020

OOPS Python : Create Class

class Country():
    # Class object Attribute
    area = 3287000
    def __init__(self,name,population):
        self.population = population
        self.name=name
    def describe(self):
        print("population of {} is {}".format(self.name,self.population))
        #print("Area sq km of {} is {}".format(self.name,self.area)) also
        print("Area sq km of {} is {}".format(self.name,Country.area))
       
    def update_population(self,new_population):
        self.population = new_population
       
c1 = Country('India',1300000000)
c1.update_population(1350000000)
c1.describe()

Thursday, April 2, 2020

Theano prompts to install m2w64-toolchain to improve performance

WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded.


Steps to Install in Anaconda:
  • conda install msys2/m2w64-toolchain
  • anaconda search -t conda msys2/m2w64-toolchain
  • anaconda show msys2/m2w64-toolchain
  • conda install --channel https://conda.anaconda.org/msys2 m2w64-toolchain

Indexer for search engine in python

import sys
import os
import urllib
import re
from bs4 import BeautifulSoup
#first time
#from whoosh.fields import Schema, TEXT, KEYWORD, ID, STORED ,NUMERIC
#from whoosh.index import create_in, open_dir
#from whoosh.query import *
import whoosh.index as index
ix = index.open_dir("index")
#then onwards
from whoosh.analysis.tokenizers import RegexTokenizer
from whoosh.analysis.tokenizers import IDTokenizer
#--------------------------------------------------------------------------------------
#creating the schema
schema = Schema(lid= TEXT(stored=True),ldata=TEXT(stored=True))
#creating the index
if not os.path.exists("index"):
    os.mkdir("index")
ix = create_in("index",schema)
ix = open_dir("index")
writer = ix.writer()
#--------------------------------------------------------------------------------------
soup = BeautifulSoup(open("39/390099"))
#tag = soup.find('title')
#print(tag.text)
#print(soup.prettify())
#for link in soup.find_all('a'):
#   print(link.get('href'))
#print(soup.get_text())
text1=soup.get_text()
text2=text1.split()

def LowercaseFilter(tokens):
    for t in tokens:
        t.text = t.text.lower()
        yield t

regt = RegexTokenizer()
idt = IDTokenizer()
#-----------------------------------------------------------------------------

for token in regt(text1):
   writer.add_document(lid=unicode(LowercaseFilter(token.text.encode('utf-8'))),ldata=unicode(LowercaseFilter(token.text.encode('utf-8'))))
   break
   #print unicode(token.text.encode('utf-8'))
  
#length=len(words_list)


 
   #print unicode(token) , fdist1[token]
 
   #print words_list[i]


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

testVar=raw_input("enter a search keyword : ")
from whoosh.qparser import QueryParser
ix.searcher().documents()
with ix.searcher() as searcher:
    #query = QueryParser("ldata", ix.schema).parse(u'methylotrophus')
    query = QueryParser("ldata", ix.schema).parse(testVar)
    results = searcher.search(query)
    for result in results:
        print result

are python strings mutable ?

No python strings are not mutable.
That means you cannot use indexing to change individual characters in a string in python.

pip install to a local folder , pip install from a local folder

pip install to a local folder 
pip download --dest localpip/ --find-links localpip/ requests

pip install from a local folder
pip --find-links localpip/ numpy

apt-get install to a folder of your choice

sudo apt-get install --download-only 'required-library' -o Dir::Cache::archives='path to your folder'

example path = '/home/ubuntu/test/'
example required library =  numpy