BVB Source Codes

TextBlob Show __init__.py Source code

Return Download TextBlob: download __init__.py Source code - Download TextBlob Source code - Type:.py
  1. # -*- coding: utf-8 -*-
  2. import csv
  3. from textblob.compat import izip
  4.  
  5. #http://semver.org/
  6. VERSION = (0, 9, 4)
  7. __version__ = ".".join(map(str,VERSION))
  8.  
  9. pass_throughs = [
  10.     'register_dialect',
  11.     'unregister_dialect',
  12.     'get_dialect',
  13.     'list_dialects',
  14.     'field_size_limit',
  15.     'Dialect',
  16.     'excel',
  17.     'excel_tab',
  18.     'Sniffer',
  19.     'QUOTE_ALL',
  20.     'QUOTE_MINIMAL',
  21.     'QUOTE_NONNUMERIC',
  22.     'QUOTE_NONE',
  23.     'Error'
  24. ]
  25. __all__ = [
  26.     'reader',
  27.     'writer',
  28.     'DictReader',
  29.     'DictWriter',
  30. ] + pass_throughs
  31.  
  32. for prop in pass_throughs:
  33.     globals()[prop]=getattr(csv, prop)
  34.  
  35. def _stringify(s, encoding, errors):
  36.     if s is None:
  37.         return ''
  38.     if isinstance(s, unicode):
  39.         return s.encode(encoding, errors)
  40.     elif isinstance(s, (int , float)):
  41.         pass #let csv.QUOTE_NONNUMERIC do its thing.
  42.     elif not isinstance(s, str):
  43.         s=str(s)
  44.     return s
  45.  
  46. def _stringify_list(l, encoding, errors='strict'):
  47.     try:
  48.         return [_stringify(s, encoding, errors) for s in iter(l)]
  49.     except TypeError as e:
  50.         raise csv.Error(str(e))
  51.  
  52. def _unicodify(s, encoding):
  53.     if s is None:
  54.         return None
  55.     if isinstance(s, (unicode, int, float)):
  56.         return s
  57.     elif isinstance(s, str):
  58.         return s.decode(encoding)
  59.     return s
  60.  
  61. class UnicodeWriter(object):
  62.     """
  63.    >>> import unicodecsv
  64.    >>> from cStringIO import StringIO
  65.    >>> f = StringIO()
  66.    >>> w = unicodecsv.writer(f, encoding='utf-8')
  67.    >>> w.writerow((u'茅', u'帽'))
  68.    >>> f.seek(0)
  69.    >>> r = unicodecsv.reader(f, encoding='utf-8')
  70.    >>> row = r.next()
  71.    >>> row[0] == u'茅'
  72.    True
  73.    >>> row[1] == u'帽'
  74.    True
  75.    """
  76.     def __init__(self, f, dialect=csv.excel, encoding='utf-8', errors='strict',
  77.                  *args, **kwds):
  78.         self.encoding = encoding
  79.         self.writer = csv.writer(f, dialect, *args, **kwds)
  80.         self.encoding_errors = errors
  81.  
  82.     def writerow(self, row):
  83.         self.writer.writerow(_stringify_list(row, self.encoding, self.encoding_errors))
  84.  
  85.     def writerows(self, rows):
  86.         for row in rows:
  87.           self.writerow(row)
  88.  
  89.     @property
  90.     def dialect(self):
  91.         return self.writer.dialect
  92. writer = UnicodeWriter
  93.  
  94. class UnicodeReader(object):
  95.     def __init__(self, f, dialect=None, encoding='utf-8', errors='strict',
  96.                  **kwds):
  97.         format_params = ['delimiter', 'doublequote', 'escapechar', 'lineterminator', 'quotechar', 'quoting', 'skipinitialspace']
  98.         if dialect is None:
  99.             if not any([kwd_name in format_params for kwd_name in kwds.keys()]):
  100.                 dialect = csv.excel
  101.         self.reader = csv.reader(f, dialect, **kwds)
  102.         self.encoding = encoding
  103.         self.encoding_errors = errors
  104.  
  105.     def next(self):
  106.         row = self.reader.next()
  107.         encoding = self.encoding
  108.         encoding_errors = self.encoding_errors
  109.         float_ = float
  110.         unicode_ = unicode
  111.         return [(value if isinstance(value, float_) else
  112.                  unicode_(value, encoding, encoding_errors)) for value in row]
  113.  
  114.     def __iter__(self):
  115.         return self
  116.  
  117.     @property
  118.     def dialect(self):
  119.         return self.reader.dialect
  120.  
  121.     @property
  122.     def line_num(self):
  123.         return self.reader.line_num
  124. reader = UnicodeReader
  125.  
  126. class DictWriter(csv.DictWriter):
  127.     """
  128.    >>> from cStringIO import StringIO
  129.    >>> f = StringIO()
  130.    >>> w = DictWriter(f, ['a', u'帽', 'b'], restval=u'卯')
  131.    >>> w.writerow({'a':'1', u'帽':'2'})
  132.    >>> w.writerow({'a':'1', u'帽':'2', 'b':u'酶'})
  133.    >>> w.writerow({'a':u'茅', u'帽':'2'})
  134.    >>> f.seek(0)
  135.    >>> r = DictReader(f, fieldnames=['a', u'帽'], restkey='r')
  136.    >>> r.next() == {'a': u'1', u'帽':'2', 'r': [u'卯']}
  137.    True
  138.    >>> r.next() == {'a': u'1', u'帽':'2', 'r': [u'\xc3\xb8']}
  139.    True
  140.    >>> r.next() == {'a': u'\xc3\xa9', u'帽':'2', 'r': [u'\xc3\xae']}
  141.    True
  142.    """
  143.     def __init__(self, csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', encoding='utf-8', errors='strict', *args, **kwds):
  144.         self.encoding = encoding
  145.         csv.DictWriter.__init__(self, csvfile, fieldnames, restval, extrasaction, dialect, *args, **kwds)
  146.         self.writer = UnicodeWriter(csvfile, dialect, encoding=encoding, errors=errors, *args, **kwds)
  147.         self.encoding_errors = errors
  148.  
  149.     def writeheader(self):
  150.         fieldnames = _stringify_list(self.fieldnames, self.encoding, self.encoding_errors)
  151.         header = dict(zip(self.fieldnames, self.fieldnames))
  152.         self.writerow(header)
  153.  
  154. class DictReader(csv.DictReader):
  155.     """
  156.    >>> from cStringIO import StringIO
  157.    >>> f = StringIO()
  158.    >>> w = DictWriter(f, fieldnames=['name', 'place'])
  159.    >>> w.writerow({'name': 'Cary Grant', 'place': 'hollywood'})
  160.    >>> w.writerow({'name': 'Nathan Brillstone', 'place': u'酶Land'})
  161.    >>> w.writerow({'name': u'Willam 酶. Unicoder', 'place': u'茅Spandland'})
  162.    >>> f.seek(0)
  163.    >>> r = DictReader(f, fieldnames=['name', 'place'])
  164.    >>> print r.next() == {'name': 'Cary Grant', 'place': 'hollywood'}
  165.    True
  166.    >>> print r.next() == {'name': 'Nathan Brillstone', 'place': u'酶Land'}
  167.    True
  168.    >>> print r.next() == {'name': u'Willam 酶. Unicoder', 'place': u'茅Spandland'}
  169.    True
  170.    """
  171.     def __init__(self, csvfile, fieldnames=None, restkey=None, restval=None,
  172.                  dialect='excel', encoding='utf-8', errors='strict', *args,
  173.                  **kwds):
  174.         if fieldnames is not None:
  175.             fieldnames = _stringify_list(fieldnames, encoding)
  176.         csv.DictReader.__init__(self, csvfile, fieldnames, restkey, restval, dialect, *args, **kwds)
  177.         self.reader = UnicodeReader(csvfile, dialect, encoding=encoding,
  178.                                     errors=errors, *args, **kwds)
  179.         if fieldnames is None and not hasattr(csv.DictReader, 'fieldnames'):
  180.             # Python 2.5 fieldnames workaround. (http://bugs.python.org/issue3436)
  181.             reader = UnicodeReader(csvfile, dialect, encoding=encoding, *args, **kwds)
  182.             self.fieldnames = _stringify_list(reader.next(), reader.encoding)
  183.         self.unicode_fieldnames = [_unicodify(f, encoding) for f in
  184.                                    self.fieldnames]
  185.         self.unicode_restkey = _unicodify(restkey, encoding)
  186.  
  187.     def next(self):
  188.         row = csv.DictReader.next(self)
  189.         result = dict((uni_key, row[str_key]) for (str_key, uni_key) in
  190.                       izip(self.fieldnames, self.unicode_fieldnames))
  191.         rest = row.get(self.restkey)
  192.         if rest:
  193.             result[self.unicode_restkey] = rest
  194.         return result
  195.  
download__init__.py Source code - Download TextBlob Source code
Related Source Codes/Software:
monaco-editor - A browser based code editor 2017-01-07
grumpy - Grumpy is a Python to Go source code transcompiler... 2017-01-07
flinux - Foreign LINUX - Run unmodified Linux applications ... 2017-01-07
beeswithmachineguns - A utility for arming (creating) many bees (micro E... 2017-01-07
TopDeepLearning - A list of popular github projects related to deep ... 2017-01-07
yui3 - A library for building richly interactive web appl... 2017-01-07
KineticJS - KineticJS is an HTML5 Canvas JavaScript framework ... 2017-01-07
web-design-standards - Open source UI components and visual style guide f... 2017-01-07
Knuff - The debug application for Apple Push Notification ... 2017-01-07
RoundedImageView - A fast ImageView that supports rounded corners, ov... 2017-01-07
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top