Sublime Forum

Open http / www files at once?

#12

Thank you, everything is working great; what is the command to open all the http links at the same time when fetching links after the generator ?

RegEx powerful stuff; what is the purpose of a generator ?

0 Likes

#13

You can just turn the generator into a list:

list(gen)

We created list/generator elements as the result of webbrowser.open(link), because of the desired side effect of opening the link.

Using a list all list elements will directly created and hence all links open at the same time.

Generators use lazy evaluation to retrieve each element only when it is requested, hence each link was only opened, when accessing the element. We just abused the lazy evaluation to get a lazy side-effect. In general generators are used to improve performance, because they require less memory space, are usually faster, and you can cancel them early without creating all elements.

2 Likes

#14

Can it stop loading the pages when it reaches the end of the document; rather then a continuous loop as does list(gen) ?

0 Likes

#15

The generator only goes one time from top to bottom of the document. So list(gen) won’t go into an infinite loop, but open all remaining links. Your JSON just might contain more links, than you thought.

0 Likes

#16

That is originally what I thought as well, although when watching the links open in the browser, I noticed the same links opening two or three times, that’s where I though it must be looping.

0 Likes

#17

You can replace list(view.substr(sel) for sel in view.sel()) with set(view.substr(sel) for sel in view.sel()), which will remove all duplicates before opening the links. It will mess up the order they appear in though.

0 Likes

#18

I prefer to keep the order :slightly_smiling:

0 Likes

#19

You can remove duplicates and preserve order using list(collections.OrderedDict.fromkeys(your_list).keys()).

This would result in the generator:

import collections, webbrowser; gen = (("opening '%s'" % link, webbrowser.open(link)) for link in list(collections.OrderedDict.fromkeys(view.substr(sel) for sel in view.sel()).keys()))
0 Likes

#20

I’m getting an error with the generator;

AttributeError: ‘module’ object has no attribute ‘OrderedDict’

0 Likes

#21

Stop using ST2. ST3 is the future!

2 Likes

#22

Sorry I wasn’t clear in my last post. When I run this generator;

import collections, webbrowser; gen = (("opening '%s'" % link, webbrowser.open(link)) for link in list(collections.OrderedDict.fromkeys(view.substr(sel) for sel in view.sel()).keys()))

I get an AttributeError: ‘module’ object has no attribute ‘OrderedDict’ ?

0 Likes

#23

FichteFoll has already answered that - OrderedDict is a Python 3 thing, Sublime Text 2 uses Python 2, while ST3 uses Python 3. Really, there is no reason to not be using ST3.

2 Likes

#24

Is there not a substitute for Sublime text 2 ?

0 Likes

#25
1 Like

#26

I read that OrderedDict is available in Python 2.7+ considering I have 2.7.3 installed, shouldn’t the command not work ?

0 Likes

#27

it depends which version comes with ST2 - ST uses a built in version of Python for plugins, and it doesn’t matter what version or modules might be installed on the system, ST doesn’t use/see them.

0 Likes

#28

I understand :slightly_smiling:

0 Likes

#29

OrderedDict has been added in python 2.7 and ST2 uses python 2.6.

This is a little bit ugly, but should also work in ST2:

import webbrowser; visited = set([]); gen = (("opening '%s'" % link, webbrowser.open(link)) for link in list(view.substr(sel) for sel in view.sel()) if link not in visited and not visited.add(link))
0 Likes

#30

r-stein - I assume because of the error, the following command must be changed ?

I’m getting an error ‘collections’ is not defined ?

The generator even though it’s as you say ‘ugly’ did work :slightly_smiling:

0 Likes

#31

What do you call the command after the generator is created so I can explain the problem a little better ? :slightly_smiling:

0 Likes