Truncating a string in python

It’s called a slice. From the python documentation under Common Sequence Operations: s[i:j] The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use … Read more

Truncating unicode so it fits a maximum size when encoded for wire transfer

def unicode_truncate(s, length, encoding=’utf-8′): encoded = s.encode(encoding)[:length] return encoded.decode(encoding, ‘ignore’) Here is an example for a Unicode string where each character is represented with 2 bytes in UTF-8 and that would’ve crashed if the split Unicode code point wasn’t ignored: >>> unicode_truncate(u’абвгд’, 5) u’\u0430\u0431′

Truncate multiple tables in one MySQL statement

No, you can only truncate a single table with TRUNCATE command. To truncate multiple tables you can use T-SQL and iterate through table names to truncate each at a time. DECLARE @delimiter CHAR(1), @tableList VARCHAR(MAX), @tableName VARCHAR(20), @currLen INT SET @delimiter=”,” SET @tableList=”table1,table2,table3″ WHILE LEN(@tableList) > 0 BEGIN SELECT @currLen = ( CASE charindex( @delimiter, … Read more

Truncate NSDate (Zero-out time)

unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:flags fromDate:date]; NSDate* dateOnly = [calendar dateFromComponents:components]; date is the date you want to remove the time from. This separates the date and time and creates a new date with the default time (00:00:00). EDIT To take … Read more

Truncating the first 100MB of a file in linux

Answer, now this is reality with Linux kernel v3.15 (ext4/xfs) Read here http://man7.org/linux/man-pages/man2/fallocate.2.html Testing code #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #ifndef FALLOC_FL_COLLAPSE_RANGE #define FALLOC_FL_COLLAPSE_RANGE 0x08 #endif int main(int argc, const char * argv[]) { int ret; char * page = malloc(4096); int fd = open(“test.txt”, O_CREAT | O_TRUNC | O_RDWR, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)