Hot answers tagged

12

This script might get the job done, but it is a rather crude and inefficient hack. Ideally, you should avoid polling every quarter second (or polling at all). Also, I see no reason to write any files to /tmp. The ideal way to do it in Linux is to use udev. If you don't want to write a persistent udev rule, or you don't have root access, you can run /sbin/...


10

I will offer some suggestions. Please note I have no experience with Mac OS, but I believe most of it will still apply. Using system is not particularly bad in your case since you use static arguments in those calls excluding the possibility of command injection. However, since your first C program is almost completely made up of system calls, you should ...


8

Nitpicks Maximum line length should be 72 characters for docstrings. For your old variable, you may wan to use a set instead of a list to check for existence (not in) as it will be \$O(1)\$ instead of \$O(n)\$. Reusability As it stand, I can't use this script because it is too much tied to your OSX only notifier. I'd rather yield questions from ...


5

context managers You should really open and close files with the with statement see PEP343 if __name__ == '__main__': guard Python idiom is to use a guard to ensure main is not run when being imported by another script tempfile As mentioned by @Toby already, there is module for creating Temporary files/directories Why does it need to be in a file though? ...


5

Prefer to use $TMPDIR if set, and /tmp only as a fallback. That's the standard practice that allows users to have separate, private temporary directories, for example, so don't subvert it! You probably ought to consider tempfile.​TemporaryDirectory() as an alternative. Error messages should go to standard error channel, not standard output. I don't know ...


4

There are a couple of things that bother me: 1) I don't think you have properly implemented relative and absolute paths. At least in Windows: absolute path = working directory + relative path. Your class only does this conversion correctly, if working directory is root directory, which is almost never the case in .Net. I don't know how things are in Linux, ...


4

You don't delete previous items from old and so it keeps increasing. This shouldn't have too much of an impact, but, can eat up your memory after a long while. The else: continue is also un-needed, as if it's else then it'll go to the beginning of the loop anyway.


4

As to the security of this: if [ -x /usr/local/libexec/path_helper ]; then eval `/usr/local/libexec/path_helper` fi This runs the path_helper script, which presumably outputs a string, and then the command represented by the output string is run in the local context. So this initially presumes you trust path_helper to run anything it wants. I don't ...


4

Another point I didn't see mentioned: try: os.mkdir('/tmp/dev') except FileExistsError: pass except blocks with pass are usually a sign that there is probably a better way. In this case, assuming you are using Python 3.2 or later, is to use os.makedirs with the exist_ok argument set to True: os.makedirs('/tmp/dev', exist_ok=True)


4

Limit globbing to directories only by appending a slash: for dir in */ If you cd into subdirectories, you don't need to construct a new path for each file. If you cd inside a subshell with ( cd … ), the original directory will be restored when subshell exits. Make sure to increment n outside of the subshell, or the new value will be lost! The IFS= is ...


3

A few empty lines here and there would do wonders to the readability. This will also relaunch the application if it was started from a terminal without arguments. String path = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath(); String os = System.getProperty("os.name").toLowerCase(); ... if(os.contains("mac"...


3

Usability The current script works in a dual mode: When run without parameters, it toggles group membership When run with a parameter, the parameter is executed as root, on the condition that the user is not currently an admin I find the conditional execution in the second mode a bit confusing. That is, when a parameter is given, but the user is admin, ...


3

What's with the absolute paths? I don't see much point in calling cat, tr, awk by absolute paths. What if you wanted to run the script on another system where these tools are installed somewhere else? You would have to replace all of them, carefully not miss one use. Better alternatives: Add PATH="/usr/bin:$PATH" near the top of the script Define tr=/usr/...


3

You can use open with an absolute path, it doesn't have to be a relative path. That is, you could write the same in one line, with some basic improvements: openUp() { open "$(sed 's@\(.*\)/.*@\1@' <<< "$1")/.." } The basic improvements: Instead of echo "..." | cmd, use here strings: cmd <<< "..." Double-quote variables used as command ...


3

for question in questions[:1]: is just a fancy way for writing for question in questions[0]: which is just question = questions[0]. In addition, I would make old a set. This way the if question.title not in old: check will stay O(1) and not grow with the number of seen questions. Your format can be slightly simplified by accessing the attribute in the ...


3

Two more ideas: I would switch the old.append(question.title) and the Notifier.notify-lines. It makes more sense to add the question to the list of already notified questions AFTER they have been (implicit successfully) notified. the indices in string format are redundant if used in order, I would just use Votes: {} | Answers: {} | Views: {}.


3

With this code and your example data, both a/c/0.png and b/0.png get renamed to 1_0.png. This happens because changes to $n inside ( ) get lost when you leave the subshell. Use bash's "globstar" feature to recurse for you, and assign numbers as normal. **/ matches all subdirectories. You want the current directory too, so add . to the list. This doesn't ...


3

Like you noticed, no need to use multiple loops when you can do everything in one function and loop only once. Aside from that, there are many small things that can be improved with this script: This script doesn't work if the script is not in the same directory as the source directory. Correct if me I am wrong, but this is what I encountered when I tested ...


3

Please consider follow recommendations: Make habit of extracting each struct/class/enum in separate files which brings more readability and clarity. Code looks less cluttered. Please try getting used to of not using semicolons :) You have defined Events as a class with empty implementations of functions. Are you sure you want to do that, I think it could ...


3

We can use plain POSIX /bin/sh. The only Bashism I see is if [[ "$OSTYPE" == "linux-gnu"* ]]; then elif [[ "$OSTYPE" == "darwin"* ]]; then fi This is in any case more naturally expressed as case "$(uname -s)" in Linux) ;; Darwin) # fill in actual value here ;; esac It would be ...


2

There are 2 things missing from your code. As @Hanzaplastique mentioned, you need to wait for the callback to be executed. Secondly, you need to pass a valid queue to DASessionSetDispatchQueue(). If you pass NULL as in your code, you're telling it to unschedule the session. You can use a dispatch_semaphore_t to wait between threads. Putting it all together, ...


2

A few years later, and not being a C expert; As far as I know (using Pascal myself), your application needs to wait for the callback to happen. By the looks of it though (again: not an expert) your application may already closed/finished before the callback actually happened.


2

Seems all clean and proper. But. The function gen_pw() is a bit, how shall I put it, longwinded. You can use variables in sed, too, so something like (in one line for easy C&P) r1="A-Z"; r2="0-9"; r3="" ; cat /dev/urandom | tr -cd '[:graph:]' | head -c 1000| sed -e "s/[^$r1$r2$r3]//g" | head -c 10 can be done easily If you base your program on it ...


2

The code in this answer is not tested and is only there as a guide "A badly constructed object is not better than using globals." – me def circle_radius_from_circumference(circ): return circ / math.tau def circle_circumference_from_radius(radius): return radius * math.tau How to make an object out of that ? class Circle: @staticmethod ...


2

One small thing - you can reduce the number of invocations of tput by storing the output of the tput commands rather than the command lines themselves, and then interpolating those output values when needed. Example: bgBlack=$(tput setab 0) white=$(tput setaf 7) stopAllFX=$(tput sgr0) read -p "[*] $stopAllFX$bgBlack${white}Select an interface: >&...


2

It seems you have installed Bash version 4 and used some of the new features in your script (e.g. associative arrays), so I will not restrain myself with the rein of portable shell programming. Also, I have only tested my solution on the latest openSUSE so it might not work on other operating systems. Update function is too slow Your update function takes ...


2

killall Terminal is very harsh! Maybe you aren't a serious Terminal user now, but someday, you might be doing something important in another Terminal window. And, due to the way macOS works, multiple windows all run under a single Terminal process, so you would be killing all of your Terminal windows. At most, do osascript -e 'tell application "Terminal" ...


2

I need to ask a question when I break up my player into usable components. How should I take this approach The idea is to break a long and not so readable code into smaller parts. Also, each individual part needs to access variables etc. For this you can use a private extension (unless it's in another file, then skip private): struct ContentView: View { ...


1

Replace magic numbers like 13 there line[:13] with constansts. IMHO: rename ids to lines all_ids to ids processes to pids


1

This is an interesting project. Is your goal to simplify the use of CoreGraphics from Python? What's the advantage of using your python module over just importing Quartz and using it directly? That's not intended as a criticism, but a serious question. The answer will inform how you design the interface for your library. Naming One thing I'd like to point ...


Only top voted, non community-wiki answers of a minimum length are eligible