A few days ago I wanted to build a tools to cut a long text into one hundred-letters strings with the number of the tweets and which part of the tweets the tweet belongs to.

I tried to have a tweet with one-hundred characters long but I could not find anything relevant to [1/x]

This is my tool:

import tweepy

n = 100

with open('texte-tw.txt','r') as input_file:
abc = []
for lines in input_file:
abc.extend([lines[i:i+n] for i in range(0,len(lines),n)])

abc = [i.replace('\n','') for i in abc]

abc = list(filter(bool, abc))

ckey = 'xxx'
csecret = 'yyy'
atoken = 'uuu'
asecret = 'zzz'

auth = tweepy.OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
api = tweepy.API(auth)

for i,val in enumerate(abc):
api.update_status(status=(val+' ['+str(i+1)+'/'+str(len(abc))+']'))


This is an extract of my file:

While it should be clear that we need a means of representing and reasoningabout uncertainty, it is not immediately obvious that probability theory can provideall of the tools we want for artiﬁcial intelligence applications.

Probability theorywas originally developed to analyze the frequencies of events. It is easy to seehow probability theory can be used to study events like drawing a certain hand ofcards in a game of poker.

These kinds of events are often repeatable. When wesay that an outcome has a probabilitypof occurring, it means that if we repeatedthe experiment (e.g., draw a hand of cards) inﬁnitely many times, then proportionpof the repetitions would result in that outcome.


Questions are the following:

• What can I do to improve my solution?
• Is there a way to cut with words that will be around one-hundred characters (+/- 10 characters) long tweet?
• Are there way to package everything and that the solution will run with just one click (using Tkinter or something similar)?

My feedback:

Separation of concerns

You have three main concerns on your program:

1. Retrieving the input data (the text)
2. Splitting the text into 100 character tweets
3. Outputting the data to twitter

Here, you can have three functions, one per concern. Once you separate those concerns, your final code will become more flexible. You'll be able to (for example) take data from a different place than a file (e.g. a text field on GUI), output to some place different than twitter (e.g. you may want to output to the console for debugging), test different splitting algorithms (e.g. you may try an algorithm that tries not to break any words).

def readFile(filename):
"""Returns a string with the file text"""

def splitText(text, size):
"""Splits text into chunks of length size"""

def tweetEverything(texts):
"""Tweet each text in texts, adding the index of text in texts"""


Descriptive variable names

abc can become splitted_text, val can become excerpt, n can become split_size.

Cleaner ways of doing the same

(val+' ['+str(i+1)+'/'+str(len(abc))+']'


can be done using the string method format

'{} [{}/{}]'.format(val, i+1, len(abc))


Note how by doing it this way you avoid casting to str, and you clearly put the output format on the left side and the variables you render using that format on the right side.

list(filter(bool, abc))


in this case, that is the same as:

filter(None, abc)


because the default filtering behaviour is filter out false values. Another option is to use a list comprehension:

[excerpt for excerpt in abc if excerpt]


Reference