Recently shared snippets by user "anonymous"

Created 4 years ago by anonymous
from flask import Flask, render_template, send_file, redirect
from PyPDF2 import PdfFileWriter, PdfFileReader
from os import makedirs
from os.path import isfile
from glob import glob
app = Flask(__name__)
def build_path(which, mod=None, page=None):
    return 'pdfs/{}{}{}'.format(which,
                                '.{}'.format(mod) if mod is not None else '',
                                '/{}.pdf'.format(page) if page is not None else '')
def get_saved_page(which):
    """Retrieve the last visited page of a pdf"""
    saved_path = build_path(which, 'saved')
    if not isfile(saved_path):
        set_saved_page(which, 0)
        return 0
    with open(saved_path, 'r') as f:
        return int(
def set_saved_page(which, page):
    """Set the last visited page of a pdf"""
    saved_path = build_path(which, 'saved')
    if not isfile(saved_path):
        open(saved_path, 'a').close()
    with open(saved_path, 'w') as f:
def serve_pdf(which, page):
    """Serve a page of a pdf file"""
    return send_file(build_path(which, 'pages', page))
def pdf(which, page=None):
    """Create a page of a pdf and display it"""
    if page is None:
        page = get_saved_page(which)
    if page == 'all':
        return send_file(build_path(which))
    page = int(page)
    pdf_path = build_path(which)
    page_directory = build_path(which, 'pages')
    page_path = build_path(which, 'pages', page)
    if page < 0:
        return redirect('{}/{}'.format(which, 0))
    if not isfile(page_path):
        makedirs(page_directory, exist_ok=True)
        pdfout = PdfFileWriter()
        with open(pdf_path, 'rb') as fin:
            pdfin = PdfFileReader(fin)
            if pdfin.isEncrypted:
            with open(page_path, 'wb') as fout:
    set_saved_page(which, page)
    return render_template('index.html', which=which, page=page)
def contents():
    """List all available pdfs"""
    contents = list(map(lambda x: {'href': x.split('/')[1], 'name': ' '.join(x.split('/')[1].split('.')[0:-1])}, glob('pdfs/*.pdf')))
    return render_template('contents.html', contents=contents)
if __name__ == '__main__':
Created 4 years ago by anonymous
public void testRandomEdits() throws IOException {
    List<Input> keys = new ArrayList<>();
    int numTerms = atLeast(100);
    for (int i = 0; i < numTerms; i++) {
      keys.add(new Input("boo" + TestUtil.randomSimpleString(random()), 1 + random().nextInt(100)));
    keys.add(new Input("foo bar boo far", 12));
    MockAnalyzer analyzer = new MockAnalyzer(random(), MockTokenizer.KEYWORD, false);
    Directory tempDir = getDirectory();
    FuzzySuggester suggester = new FuzzySuggester(tempDir, "fuzzy", analyzer, analyzer, FuzzySuggester.EXACT_FIRST | FuzzySuggester.PRESERVE_SEP, 256, -1, true, FuzzySuggester.DEFAULT_MAX_EDITS, FuzzySuggester.DEFAULT_TRANSPOSITIONS,
                                                  0, FuzzySuggester.DEFAULT_MIN_FUZZY_LENGTH, FuzzySuggester.DEFAULT_UNICODE_AWARE); InputArrayIterator(keys));
    int numIters = atLeast(10);
    for (int i = 0; i < numIters; i++) {
      String addRandomEdit = addRandomEdit("foo bar boo", FuzzySuggester.DEFAULT_NON_FUZZY_PREFIX);
      List<LookupResult> results = suggester.lookup(TestUtil.stringToCharSequence(addRandomEdit, random()), false, 2);
      assertEquals(addRandomEdit, 1, results.size());
      assertEquals("foo bar boo far", results.get(0).key.toString());
      assertEquals(12, results.get(0).value, 0.01F);  
    IOUtils.close(analyzer, tempDir);
// set request time out to 10 min to avoid a timeout for this request
			java.util.Map<String, Object> requestContext = (( omsQuery)
					new Integer(600000));
					new Integer(600000));
					new Integer(600000));
					new Integer(600000));
			requestContext.put("", new Integer(
Created 4 years ago by anonymous
public static void main(String[] args) {
		ArrayList<String> result = new ArrayList<String>() ;
		Map<String, List<String>> map = -> each.split("\\.")[0],
				each -> new ArrayList<String>(Arrays.asList(each.split("\\.")[1])), (oldValue, newValue) -> {
					return oldValue;
Created 4 years ago by anonymous
    fn eval(script: &Script, args: &[Value]) -> Result<Value, Error>
    = eval_impl
    in script.as_ref()

fn eval_impl(script: &Script, args: &[Value]) -> Result<Value, Error> {
    // ...
    // ... A few hundred lines or ordinary Rust code later ...
    // ...

fn main() {
    let script = ...;
    let args = ...;
    // Call it as any ordinary function.
    let res = eval(&script, &args);
    println!("Result: {}", res);