Skip to content

Instantly share code, notes, and snippets.

@Oleh-Hrebchuk
Last active March 15, 2016 16:39
Show Gist options
  • Save Oleh-Hrebchuk/84f70d776e257dbd2f1f to your computer and use it in GitHub Desktop.
Save Oleh-Hrebchuk/84f70d776e257dbd2f1f to your computer and use it in GitHub Desktop.
oop hulpa
class Lonely(object):
instance = None
def __new__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance=object.__new__(cls)
return cls.instance
def __init__(self, name, lastname):
self.name=name
self.lastname=lastname
def __str__(self):
return "name=%s : lastname=%s" % (self.name,self.lastname)
c=Lonely('oleh','hrebchuk')
b=Lonely('oleh2','hrebchuk2')
print (c)
print (b)
#output
##name=oleh2 : lastname=hrebchuk2
##name=oleh2 : lastname=hrebchuk2
class Observable():
def __init__(self, **kwargs):
self.__dict__= kwargs
def __repr__(self):
return 'Observable(titles: %s)' % (self.__dict__)
c=Observable(vasa='army',yura=('developer', 'QA'),oleh='admin')
print c.vasa
print c.yura
print c.oleh
print(c)
##output
#army
#('developer', 'QA')
#admin
#Observable(titles: {'vasa': 'army', 'yura': ('developer', 'QA'), 'oleh': 'admin'})
import re
class RegexValidator(object):
def __init__(self, regex, message):
self.regex = regex
self.message = message
def __call__(self,data):
self.data = data
macher = re.compile(self.regex)
if macher.findall(r'{0}'.format(self.data)):
print 'is valid value',self.data
else:
print RegexValidator.__module__+'.ValidationError:',self.message
class EmailValidatior(RegexValidator):
def __init__(self,regex,message):
RegexValidator.__init__(self,regex,message)
def __call__(self, data):
RegexValidator.__call__(self, data)
c=RegexValidator(regex = '^[A-Z]$', message = 'Ouch! your input is not valid!')
b=EmailValidatior(regex = '[@.]', message = 'Enter a valid email')
c('Z')
c('a')
b('oleh.hrebchuk@domen.com')
b('yura.hulpa@domen.com')
@Oleh-Hrebchuk
Copy link
Author

instead repr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment