Re: Fetch data from two dates query in python and mongoengine

2018-08-24 Thread Kunal Jamdade
Hi Mahesh,

Import Q from queryset.

from mongoengine.queryset.visitor import Q

ProcessedEmails.objects(Q(first_date) & Q(second_date))



On Fri, Aug 24, 2018 at 12:41 PM mahesh d  wrote:

> [image: Boxbe]  This message is eligible
> for Automatic Cleanup! (mahesh.tec...@gmail.com) Add cleanup rule
> 
> | More info
> 
> Hii
> My model like this
> class ProcessedEmails(Document):
>   subject = StringField(max_length=200)
> fromaddress =StringField(max_length=200)
>  dateofprocessing = StringField(max_length=25)
> How can find out the records between two dates ??
> Note: date of processing is string field
>  Mongodb database using .
> How to write the query in python by using mongoengine
>
> Thanks
> Mahesh D.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: virtualenv and ubuntu

2018-08-28 Thread Kunal Jamdade
Hi,

If you install with sudo it is installed for 2 users i.e current logged in
as well as root. So if one login as root can use the installed "env".

But if you install it with pip it will be used by the current logged in
user.

In my opinion, use pip to install virtualenv

On Tue, Aug 28, 2018 at 6:32 PM  wrote:

> [image: Boxbe]  This message is eligible
> for Automatic Cleanup! (stone.zh...@gmail.com) Add cleanup rule
> 
> | More info
> 
> Hi there,
>
> Sorry if the question is naive, I am on Ubuntu 16.04 with Python 3.5.2,
> now I want to use virtualenv, I noticed there are two ways to get
> virtualenv installed:
>
> 1) do "sudo apt-get install virtualenv"
> 2) do "pip3 install virtualenv"
>
> What is the preferred way to install virtualenv?
>
> Thanks,
> Stone
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding methods on a per instance basis

2017-05-30 Thread Kunal Jamdade
I tried to understand  overriding methods on per instance class. But i am
not getting it. Can you help me in getting the mail. Or can u suggest me at
least what should i read before reading "this topic"?

Can you explain me with one more example?

On Tue, May 16, 2017 at 9:30 AM, Nathan Ernst 
wrote:

> There is another way to do it, but it's not pretty, and I don't recommend
> it:
> >>> class Foo:
> ... pass
> ...
> >>> from functools import partial
> >>> f = Foo()
> >>> def hello(self, arg):
> ... print("hello", arg)
> ...
> >>> f.hello = partial(hello, f)
> >>> f.hello("world")
> hello world
>
> This basically relies upon binding in a closure. It's close, but not quite
> right. i.e. if you copy the instance, the function will remember the
> original and not know about the copy.
>
> On Sun, May 14, 2017 at 11:01 AM, Steve D'Aprano <
> steve+pyt...@pearwood.info
> > wrote:
>
> > It is common to add an attribute to a class, then over-ride it in the
> > instance:
> >
> >
> > class Document:
> > pagesize = "A4"
> >
> > def __init__(self, pagesize):
> > self.pagesize = pagesize
> >
> >
> > A little-known fact, not appreciated by users of less powerful OOP
> > languages: Python supports per-instance customized methods too, since
> > methods are just attributes.
> >
> >
> > py> class Parrot:
> > ... name = "Polly"
> > ... def speak(self):
> > ... return "%s wants a cracker!" % self.name
> > ...
> > py> petey = Parrot()
> > py> petey.speak()
> > 'Polly wants a cracker!'
> >
> >
> > We can shadow petey's "speak" method with a callable:
> >
> > py> petey.speak = lambda: "Who's a cheeky boy then!"
> > py> petey.speak()
> > "Who's a cheeky boy then!"
> >
> >
> > Notice that using a regular function means that we cannot access "self".
> > But
> > all is not lost! We can do so by using a method object bound to the
> > instance:
> >
> > py> from types import MethodType
> > py> petey.speak = MethodType(
> > ... lambda self: "%s is a cheeky boy!" % self.name,
> > ... petey)
> > py> petey.speak()
> > 'Polly is a cheeky boy!'
> >
> >
> >
> >
> > This can be considered a form of the Strategy design pattern. From
> > Wikipedia:
> >
> > ... the strategy pattern (also known as the policy pattern)
> > is a behavioural software design pattern that enables an
> > algorithm's behavior to be selected at runtime. The strategy
> > pattern:
> >
> > - defines a family of algorithms,
> > - encapsulates each algorithm, and
> > - makes the algorithms interchangeable within that family.
> >
> > https://en.wikipedia.org/wiki/Strategy_pattern
> >
> > the major difference being is that in the Strategy pattern there is not
> > necessarily a default implementation provided by the class:
> >
> > class Dog:
> > pass
> >
> > rover = Dog()
> > butch = Dog()
> > laddie = Dog()
> >
> > rover.do_trick = fetch_stick
> > butch.do_trick = play_dead
> > laddie.do_trick = solve_world_hunger
> >
> >
> >
> >
> > --
> > Steve
> > Emoji: a small, fuzzy, indistinct picture used to replace a clear and
> > perfectly comprehensible word.
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Script to replace contents inside the files

2017-07-23 Thread Kunal Jamdade
I have thousands of html files inside a folder. I want to replace the
filename present inside another files. Say for ex:- fileName :-
'abcd1234.html' is found inside another file say file2.html. Then I want to
remove the last 4 digits of the fileName i.e,. 'abcd1234.html' =>
'abcd.htm'.

I have tried a script . But your suggestions upon the script are welcomed.

Regards,
Kunal
import os
import re


def script_to_create_folder():
path_list = []
filename_list = []
path = r'D:\macrocodesrequired\Testing_Script\Real_testing_\New folder\brpt'
#path = r'H:\Script_Work\New_folder\Actual_testing\brpt'

for (root, dirs, name) in os.walk(path):
for nm in name:
if (
'About' in root or 'Community' in root or 'support' in root \
or 'home' in root or 'Products' in root or 'service' in root \
or 'solutions' in root or 'training' in root \
or 'wheretobuy' in root
):
pass
if (
'default' in nm or 'index' in nm or 'category' in nm \
or 'Category' in nm or 'Default' in nm or 'Index' in nm \
or 'home' in nm or 'support' in nm
):
pass
else:
filename_list.append(nm)
path_list.append(os.path.join(root, nm))
# print(path_list)
# print(filename_list)

for path in path_list:
for names in filename_list:
find_filename_inside_files(names, path)


def find_filename_inside_files(file_name, dir_path):
pattern_list = ['\d+$', '\d+\w$', '\d+-\d$', '\w\d+$', '\d\w\d\w', 
'\w\d+$', '\w\d\w\d']
data = []
replace_str = ''
read_cnt = 0
digits_to_replace = 0
with open(dir_path, 'r', encoding='utf-8') as file_handle:
data = file_handle.read()
#print(data)
if file_name in data:
#print(file_name)
for search_pattern in pattern_list:
read_cnt = 0
if '-' in file_name:
#print("===>",search_pattern)
if re.search(search_pattern, file_name.split('.')[0]):
digits_to_replace = filename_with_hypen(file_name, 
search_pattern)
read_cnt = 1
position = file_handle.tell()
replace_str = replace_oldstring_newstring(
data,
file_name,

digits_to_replace
)
# file_handle.seek(0, 0)
# file_handle.write(replace_str)
elif re.search(search_pattern, file_name.split('.')[0]):
digits_to_replace = filename_without_hypen(file_name, 
search_pattern)
read_cnt = 1
replace_str = replace_oldstring_newstring(data, file_name, 
digits_to_replace)

if read_cnt == 1:
#print("write to")
print(file_name)
print(dir_path)
with open(dir_path, 'w', encoding='utf-8') as file_out:
file_out.write(replace_str)
exit()


def filename_without_hypen(file_name, pattern):
#print(file_name)
value = re.search(pattern, file_name.split('.')[0])
if bool(value):
last_digits = value.group()
if len(last_digits) > 2:
return -(len(last_digits))
elif len(last_digits) > 0 and len(last_digits) <= 3:
return -(len(last_digits))


def filename_with_hypen(file_name, pattern):
value = re.search(pattern, file_name.split('.')[0])

if bool(value):
last_digits = value.group()
if '-2' in last_digits or '-3' in last_digits:
return -(len(last_digits))
else:
return -(len(last_digits))


def replace_oldstring_newstring(data, filename, last_digits_to_replace):
print("in replace")

ind = data.index(filename)

temp_str = data[ind:(ind + len(filename))]

replace_str = data.replace(temp_str.split('.')[0][last_digits_to_replace:], 
'')

replace_str = replace_str.replace(".html", ".htm")

return replace_str


def main():
script_to_create_folder()


if __name__ == '__main__':
main()
-- 
https://mail.python.org/mailman/listinfo/python-list


Regular expression

2017-07-26 Thread Kunal Jamdade
There is a filename say:- 'first-324-True-rms-kjhg-Meterc639.html' .

I want to extract the last 4 characters. I tried different regex. but i am
not getting it right.

Can anyone suggest me how should i proceed.?

Regards,
Kunal
-- 
https://mail.python.org/mailman/listinfo/python-list