1.1.2.15. gnr.core.gnrstring - tools for string handling

Last page update: November 23, 2011

Classes:

There is no class defined

External methods:

Complete reference:

1.1.2.15.2. gnr.core.gnrstring - The complete reference list

gnr.core.gnrstring.anyWordIn(wordlist, mystring)

Return a list of all the elements included both in wordlist and in mystring

Parameters:
  • wordlist – the list of words to be searched in mystring
  • mystring – the string on which there will be executed the search
gnr.core.gnrstring.asDict(myString, itemSep=', ', argSep='=', symbols=None)

Return a dict from a key-value like string (or an empty dict, if there is no string)

Parameters:
  • myString – a string that represent a list of key-value pairs.
  • itemSep – the separator char between each key-value pair. Default value is ,
  • argSep – the separator between keys and values. Default value is =
  • symbols – a dictionary that eventually contains value for templates in myString. Default value is False
Returns:

a dict from a key-value like string (or an empty dict, if there is no string)

>>> asDict('height=22, weight=73')
{'weight': '73', 'height': '22'}
>>> asDict('height=$myheight, weight=73', symbols={'myheight':55})
{'weight': '73', 'height': '55'}
gnr.core.gnrstring.boolean(obj)

Return False if the given obj string is one of the following values:

  • ‘n’
  • ‘no’
  • ‘f’
  • ‘false’
  • ‘0’

(the obj is lowered before comparing it)

Parameters:obj – The given object
gnr.core.gnrstring.concat(s1, s2, sep='.')

join two strings through the separator attribute sep. If the first string is None, return the second string.

Parameters:
  • s1 – the first string
  • s2 – the second string
  • sep – separation character. Default value is .
Returns:

a string with the two strings joined

gnr.core.gnrstring.countOf(myString, srcString)

Return the number of the recurrence of the srcString string into the myString string

Parameters:
  • myString – the string to be compared with srcString
  • srcString – the string to compare
Returns:

the number of the recurrence of the srcString into myString

>>> a = 'hello hello heeeello hello helo hi'
>>> b = 'hello'
>>> countOf(a,b)
3
gnr.core.gnrstring.dotEsc(txt)

Return a text with all dot char escaped

Parameters:txt – the text
Returns:the text with all dot char escaped
gnr.core.gnrstring.encode(number, base='/16', nChars=None)

Return a string that contains the given number in the specified base

Parameters:
  • number – number to encode
  • base – base of encoding. Default value is /16
  • nChar – number of characters of the result.
gnr.core.gnrstring.filter(item, include=None, exclude=None, wildcard='%')

TODO

Parameters:
  • item – TODO
  • include – TODO.
  • exclude – TODO.
  • wildcard – TODO. Default value is %
Returns:

TODO

gnr.core.gnrstring.fromIsoDate(datestring)

TODO

Parameters:datestring – TODO
gnr.core.gnrstring.fromJson(obj)

TODO

Parameters:obj – TODO
gnr.core.gnrstring.fromText(mystring, obj, locale=None)

TODO

Parameters:
  • mystring – TODO
  • obj – TODO
  • locale – the current locale (e.g: en, en_us, it)
gnr.core.gnrstring.getBetween(myString, startChunk, endChunk)

Return a string between two given chunks.

Parameters:
  • myString – the string to be checked
  • startChunk – substring that bounds the result string from left
  • endChunk – substring that bounds the result string from right
Returns:

a string between two given chunks

>>> getBetween('teststring', 'st','in')
'str'
>>> getBetween('teststring', 'st','te')
''
>>> getBetween('teststring', 'te','te')
''
gnr.core.gnrstring.getFrom(myString, chunk)

Return a string after a given chunk

Parameters:
  • myString – the string to be checked
  • chunk – substring that bounds the result string
Returns:

a string after a given chunk

>>> getFrom('teststring', 'st')
'string'
>>> getFrom('teststring', 'te')
'ststring'
>>> getFrom('teststring', 'co')
''
gnr.core.gnrstring.getFromLast(myString, chunk)

Return a string after the last occurence of a given chunk.

Parameters:
  • myString – the string to be checked
  • chunk – substring that bounds the result string
Returns:

a string after the last occurence of a given chunk

>>> getFromLast('teststring', 'st')
'ring'
>>> getFromLast('teststring', 'ng')
''
>>> getFromLast('teststring', 'co')
''
gnr.core.gnrstring.getUntil(myString, chunk, returnOriginal=False)

Search in a string a chunk: if it is present, return all the characters before this chunk. If not, return an empty string (or the original one, if returnOriginal is True).

Parameters:
  • myString – the string to be checked
  • chunk – substring that bounds the result string
  • returnOriginal – if True, return the entire string if the chunk is not present in the string. Default value is False.
Returns:

a string until a given chunk

>>> getUntil('teststring', 'st')
'te'
>>> getUntil('teststring', 'te')
''
>>> getUntil('teststring', 'co')
''
>>> getUntil('teststring', 'co', returnOriginal=True)
'teststring'
gnr.core.gnrstring.getUntilLast(myString, chunk)

Return a string until last occurence of a given chunk

Parameters:
  • myString – the string to be checked
  • chunk – substring that bounds the result string
Returns:

a string until last occurence of a given chunk

>>> getUntilLast('teststring', 'st')
'test'
>>> getUntilLast('teststring', 'te')
''
>>> getUntilLast('teststring', 'co')
''
gnr.core.gnrstring.guessLen(dtype, locale=None, format=None, mask=None, encoding=None)

TODO

Parameters:
  • dtype – the dtype
  • locale – the current locale (e.g: en, en_us, it)
  • format – TODO
  • mask – TODO
  • encoding – The multibyte character encoding you choose
gnr.core.gnrstring.ilike(s1, s2, wildcard='%')

Return True if ... Return False if ... TODO

Parameters:
  • s1 – first string
  • s2 – second string
Wildcard :

a special string. Default value is %

Returns:

TODO

gnr.core.gnrstring.jsquote(str_or_unicode)

TODO

Parameters:str_or_unicode – the string to be quoted
Returns:TODO
>>> print jsquote('pippo')
'pippo'
>>> print jsquote(u'pippo')
'pippo'
gnr.core.gnrstring.like(s1, s2, wildcard='%')

Return True if ... Return False if ... TODO

Parameters:
  • s1 – first string
  • s2 – second string
Wildcard :

a special string. Default value is %

Returns:

TODO

>>> like('*dog*', 'adogert', '*')
True
>>> like('dog*', 'adogert', '*')
False
>>> like('*dog', '*adogert', '*')
False
gnr.core.gnrstring.makeSet(*args, **kwargs)

TODO

gnr.core.gnrstring.pickleObject(obj, zipfilename=None)

Return the Pickle string for the given object

Parameters:
  • obj – The given object
  • zipfilename – TODO
gnr.core.gnrstring.regexDelete(myString, pattern)

Return a string obtained deleting from the given string any occurrency of the given pattern.

Parameters:
  • myString – the string to be shortened
  • pattern – pattern of chars that must be deleted from myString
Returns:

a string

>>> regexDelete('When he turns the steering wheel', 'he')
'Wn  turns t steering wel'
gnr.core.gnrstring.slugify(value)

TODO

Parameters:value – TODO
gnr.core.gnrstring.smartjoin(mylist, on)

Join a list with the separator substring on escaping each occurrency of on within the given list

Parameters:
  • mylist – the list to be joined
  • on – separator substring
Returns:

the joined string

>>> smartjoin(['Hello, dog', 'you', 'are', 'yellow'], ',')
'Hello\, dog,you,are,yellow'
gnr.core.gnrstring.smartsplit(path, on)

Split the string path with the separator substring on ignoring the escaped separator chars

Parameters:
  • path – the path to be splitted
  • on – separator substring
gnr.core.gnrstring.split(path, sep='.')

Return a list splitting a path string at any occurrency of separation character.

Parameters:
  • path – the path string to be splitted
  • sep – separation character. Default value is .
>>> split('first.second.third')
['first', 'second', 'third']
gnr.core.gnrstring.splitAndStrip(myString, sep=', ', n=-1, fixed=0)

Split a string in a list of n+1 items, striping white spaces.

Parameters:
  • myString – the string to be splitted
  • sep – separation character. Default value is ,
  • n – how many split operations must be done on myString. Default value is -1
  • fixed – use fixed if the resulting list must have a fixed length. Default value is 0
>>> splitAndStrip('cola, beer, milk')
['cola', 'beer', 'milk']
>>> splitAndStrip('cola, beer, milk', n=2)
['cola', 'beer, milk']
gnr.core.gnrstring.splitLast(myString, chunk)

Return a tuple of two strings created by splitting the string at the last occurence of a given chunk. If the chunk is not included in the string, return a tuple of two strings, with myString as the first one and with an empty string as the second one.

Parameters:
  • myString – string to be splitted
  • chunk – substring that bounds the result string
Returns:

a tuple of two strings

>>> splitLast('hello my dear friend', 'e')
('hello my dear fri', 'nd')
>>> splitLast(a, 'w')
('Hello my dear friend', '')
gnr.core.gnrstring.stringDict(myDict, itemSep=', ', argSep='=')

Return a string of key-value pairs taken from a dictionary.

Parameters:
  • myDict – dictionary to transform in a string
  • itemSep – the separator char between each key-value pair. Default value is ,
  • argSep – the separator between keys and values. Default value is =
Returns:

a string of key-value pairs taken from a dictionary

>>> stringDict({'height':22,'width':33})
 'width=33,height=22'
gnr.core.gnrstring.templateReplace(myString, symbolDict=None, safeMode=False, noneIsBlank=True, locale=None, formats=None, masks=None, localizer=None)

Allow to replace string’s chunks.

Parameters:
  • myString – template string
  • symbolDict – dictionary that links symbol and values. .
  • safeMode – if True (False) uses the safe_substitute() (substitute()) Python method. Default value is False.
  • noneIsBlank – TODO. Default value is True
>>> templateReplace('$foo loves $bar but she loves $aux and not $foo', {'foo':'John','bar':'Sandra','aux':'Steve'})
'John loves Sandra but she loves Steve and not John'
gnr.core.gnrstring.toJson(obj)

TODO

Parameters:obj – TODO
gnr.core.gnrstring.toJsonJS(obj)

TODO

Parameters:obj – TODO
gnr.core.gnrstring.toSecureJsonJS(obj, key=None)

TODO

Parameters:
  • obj – TODO
  • key – TODO
gnr.core.gnrstring.toText(obj, locale=None, format=None, mask=None, encoding=None, currency=None)

Return a unicode string representing an object of any class

If there are locale or format parameters Babel is used to format the value according to the given localization or format

Parameters:
  • obj – the object to be transformed in a string
  • locale – the current locale (e.g: en, en_us, it)
  • format – TODO
  • mask – TODO
  • encoding – The multibyte character encoding you choose
  • currency – TODO
gnr.core.gnrstring.unpickleObject(objstr, zipfilename=None)

Load an object from a pickle string and return it

Parameters:
  • objstr – The given object string
  • zipfilename – TODO
gnr.core.gnrstring.unzipString(mystring, filename)

Extract a zip compressed string and return it

Parameters:
  • mystring – the compressed string to decompress
  • filename – the name of the unzipped file
gnr.core.gnrstring.updateString(source, s, sep=', ')

Append a new s string to the source string. The two strings are linked by a sep char. If there isn’t source string, return the s string.

Parameters:
  • source – initial string
  • s – string to be added
  • sep – separator string. Default value is ,
>>> updateString('I drink cola', 'beer')
'I drink cola,beer'
>>> updateString('I drink cola', 'beer', ' and ')
'I drink cola and beer'
gnr.core.gnrstring.updateStringList(s1, s2, sep=', ')

TODO

Parameters:
  • s1 – TODO
  • s2 – TODO
  • sep – separator string. Default value is ,
Returns:

TODO

gnr.core.gnrstring.wordSplit(text)

Return a list that contains the words of the given text

Parameters:text – text to be splitted
Returns:a list that contains the words of the given text
>>> wordSplit('hello, my dear friend')
['hello', 'my', 'dear', 'friend']
gnr.core.gnrstring.zipString(mystring, filename)

Return a zip compressed version of the mystring string

Parameters:
  • mystring – The given string
  • filename – name of the zipped file